使用opencv计算多个面积

q3aa0525  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(158)

我尝试使用opencv的contourArea计算图片中的不同区域,但没有成功。

我的目标是计算table的空闲区域(灰色)和占用区域(橙色对象),到目前为止,我已经使用以下代码打印出了轮廓:

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-给定多个轮廓,我不知道返回的面积是表的,对象的,还是两者都有。
有什么建议吗?
非常感谢。

06odsfpq

06odsfpq1#

自从你转换到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()

编辑
计算百分比:

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

相关问题