用opencv计算多个区域

时间:2018-10-11 13:31:11

标签: python opencv image-processing

我正在尝试使用opencv的contourArea计算图片中的不同区域,但没有成功。我用作示例的图片如下:

enter image description here

我的目标是计算表的自由区域(灰色)和占用区域(橙色对象),到目前为止,已成功使用以下代码打印轮廓:

img = cv2.imread('table.jpg', 1)

b,g,r = cv2.split(img)
imgRGB = cv2.merge([r,g,b])

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hsv_channels = cv2.split(hsv)

rows = img.shape[0]
cols = img.shape[1]

for i in range(0, rows):
    for j in range(0, cols):
        h = hsv_channels[1][i][j]

        if h > 90 and h < 120:
            hsv_channels[2][i][j] = 255
        else:
            hsv_channels[2][i][j] = 0

image, contours, hierarchy = cv2.findContours(hsv_channels[2],cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
img1 = cv2.drawContours(imgRGB, contours, -1, (0,255,0), 3)

但是,我面临两个问题:

1-该代码检测圆内部的轮廓。

2-给定多个轮廓,我不知道返回的区域是表的,对象的还是两者。

有什么建议吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

自从转换到HSV色彩空间以来,您是否考虑过cv2.inRange()?之后,您可以使用cv2.findContours()找到轮廓并将其从图像中绘制出来,仅保留灰色区域。

示例:

import cv2
import numpy as np

img = cv2.imread('tablest.jpg')

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower = np.array([0,0,50])
upper = np.array([160,255,255])

mask = cv2.inRange(hsv, lower, upper)

res = cv2.bitwise_and(hsv,hsv, mask= mask)
gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

for i in contours:
    cnt = cv2.contourArea(i)
    if cnt > 1000:  
        cv2.drawContours(img, [i], 0, (0,0,0), -1)

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = max(contours, key=cv2.contourArea)
area = cv2.contourArea(cnt)
cv2.putText(img,'Gray area ='+str(area),(60,90), cv2.FONT_HERSHEY_COMPLEX, 0.5,(0,255,0),1,cv2.LINE_AA)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

enter image description here

编辑

用于计算百分比:

import cv2
import numpy as np

img = cv2.imread('tablest.jpg')

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower = np.array([0,0,50])
upper = np.array([160,255,255])

# Calculate whole area
h,w = img.shape[:2]
whole_area_mask = np.ones((h, w), np.uint8)
ret, thresh = cv2.threshold(whole_area_mask,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = max(contours, key=cv2.contourArea)
whole_area = cv2.contourArea(cnt)

# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower, upper)

# Bitwise-AND mask and original image
res = cv2.bitwise_and(hsv,hsv, mask= mask)
gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

other_area = []
table_area = []

for i in contours:
    cnt = cv2.contourArea(i)
    M = cv2.moments(i)
    cx = int(M['m10']/M['m00'])
    if cnt > 1000:  
        cv2.drawContours(img, [i], 0, (0,0,0), -1)
        if w-100 > cx > 100:
            other_area.append(cnt)
        else:
            table_area.append(cnt)

# Percentage table/napkin/object 1/object 2
table_per = (100*(table_area[0]+table_area[1]))/whole_area
print('Table percentage: ', table_per)
napkin_per = (100*(whole_area-other_area[0]-other_area[1]-table_area[0]-table_area[1]))/whole_area
print('Napkin percentage: ', napkin_per)
first_object_per = (100*other_area[0])/whole_area
print('First object percentage: ', first_object_per)
second_object_per = (100*other_area[1])/whole_area
print('Second object percentage: ', second_object_per)
print('SUM: ', table_per+napkin_per+first_object_per+second_object_per)


cv2.imshow('img', img)
cv2.imwrite('tablest_res.png', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出:

表百分比:9.875440996472028

餐巾纸百分比:58.93872849017208

第一个对象百分比:28.05565555475556

第二个对象百分比:3.1301749586003313

总和:100.0