我有一个图像从模拟我执行(图像是附加的)。我想在平板中心绘制红色轮廓,并考虑平板尺寸为1X1 cm,计算其面积。
我曾经尝试过使用Python和OpenCV,但都失败了。有人能帮忙吗?
13z8s7eq1#
首先你要一个“面具”然后你可以计算掩码中元素的数量。
image = ...(height, width) = image.shape[:2]hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)# hue, saturation, value# the red area is well saturated and very bright# red is near 0 degrees of hue# since we're on a circle, those values might span 340 to 20 degrees# hues are mapped into 0..180 (uint8) for 0..360 degrees# we need two ranges because we're spanning across 0 from both sides and inRange() can't handle that at oncemask1 = cv.inRange(hsv, (170, 128, 128), (180, 255, 255)) # pinkish redmask2 = cv.inRange(hsv, ( 0, 128, 128), ( 10, 255, 255)) # yellowish redmask = mask1 | mask2red_count = cv.countNonZero(mask)fraction = red_count / (width * height)print(f"{fraction:.2%}")# 0.76%
image = ...
(height, width) = image.shape[:2]
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
# hue, saturation, value
# the red area is well saturated and very bright
# red is near 0 degrees of hue
# since we're on a circle, those values might span 340 to 20 degrees
# hues are mapped into 0..180 (uint8) for 0..360 degrees
# we need two ranges because we're spanning across 0 from both sides and inRange() can't handle that at once
mask1 = cv.inRange(hsv, (170, 128, 128), (180, 255, 255)) # pinkish red
mask2 = cv.inRange(hsv, ( 0, 128, 128), ( 10, 255, 255)) # yellowish red
mask = mask1 | mask2
red_count = cv.countNonZero(mask)
fraction = red_count / (width * height)
print(f"{fraction:.2%}")
# 0.76%
是的,没错。0.76%,面积不大。这是根据图片裁剪计算的蒙版:
xwmevbvl2#
1.找出红色的上限和下限。1.使用cv2.inRange()获取白色蒙版。The link to use inRange function1.现在您已经有了一个遮罩,使用cv2.findContours来获取所需的轮廓,并在迭代时使用cv2.contourArea()获取面积
2条答案
按热度按时间13z8s7eq1#
首先你要一个“面具”
然后你可以计算掩码中元素的数量。
是的,没错。0.76%,面积不大。
这是根据图片裁剪计算的蒙版:
xwmevbvl2#
1.找出红色的上限和下限。
1.使用cv2.inRange()获取白色蒙版。The link to use inRange function
1.现在您已经有了一个遮罩,使用cv2.findContours来获取所需的轮廓,并在迭代时使用cv2.contourArea()获取面积