从图像中删除黄色矩形
Remove Yellow rectangle from image
我正在使用此代码从图像中删除这个黄色标记:
import cv2
import numpy as np
# read image
img = cv2.imread('input.jpg')
# threshold on yellow
lower = (0, 200, 200)
upper = (100, 255, 255)
thresh = cv2.inRange(img, lower, upper)
# apply dilate morphology
kernel = np.ones((9, 9), np.uint8)
mask = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)
# get largest contour
contours = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(big_contour)
# draw filled white contour on input
result = img.copy()
cv2.drawContours(result, [big_contour], 0, (255, 255, 255), -1)
cv2.imwrite('yellow_removed.png', result)
# show the images
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
我收到以下错误:
big_contour = max(contours, key=cv2.contourArea) ValueError: max() arg
is an empty sequence
显然,它没有检测到任何轮廓,并且轮廓数组是空的,但我不知道为什么会这样或如何修复它。
感谢您的帮助!
检查您的下限。当我将下限阈值更改为 lower = (0, 120, 120)
.
时,它对我的两个图像都有效
阈值是第二张图片较暗的原因。降低这些阈值可以捕获更多黄色区域,但在绘制轮廓时仍会留下一些孔。
lower = (0, 130, 130)
您可以通过绘制边界矩形来解决此问题。
cv2.rectangle(result,(x,y),(x+w,y+h),(255,255,255),-1)
使用 HSV 颜色 space 非常适合找出特定的 shade/tone 颜色。当你有主色要隔离时,你可以选择 LAB 颜色 space。我已经解释了为什么这样更好 .
代码:
img = cv2.imread('bill.jpg')
# create another copy for the result
img2 = img.copy()
# convert to LAB space and store b-channel
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
b_channel = lab[:,:,-1]
注意上面黄色区域的亮度。
# Perform Otsu threshold
th = cv2.threshold(b_channel, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
# Find the contour with largest area
contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
c = max(contours, key = cv2.contourArea)
# draw the contour on plain black image of same shape as original
mask = np.zeros((img.shape[0], img.shape[1]), np.uint8)
mask = cv2.drawContours(mask,[c],0,255, -1)
# dilation to avoid border effects
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
dilate = cv2.dilate(mask, kernel, iterations=1)
img2[dilate == 255] = (255, 255, 255)
另一个例子:
输入:
结果:
我正在使用此代码从图像中删除这个黄色标记:
import cv2
import numpy as np
# read image
img = cv2.imread('input.jpg')
# threshold on yellow
lower = (0, 200, 200)
upper = (100, 255, 255)
thresh = cv2.inRange(img, lower, upper)
# apply dilate morphology
kernel = np.ones((9, 9), np.uint8)
mask = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)
# get largest contour
contours = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(big_contour)
# draw filled white contour on input
result = img.copy()
cv2.drawContours(result, [big_contour], 0, (255, 255, 255), -1)
cv2.imwrite('yellow_removed.png', result)
# show the images
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
我收到以下错误:
big_contour = max(contours, key=cv2.contourArea) ValueError: max() arg is an empty sequence
显然,它没有检测到任何轮廓,并且轮廓数组是空的,但我不知道为什么会这样或如何修复它。 感谢您的帮助!
检查您的下限。当我将下限阈值更改为 lower = (0, 120, 120)
.
阈值是第二张图片较暗的原因。降低这些阈值可以捕获更多黄色区域,但在绘制轮廓时仍会留下一些孔。
lower = (0, 130, 130)
您可以通过绘制边界矩形来解决此问题。
cv2.rectangle(result,(x,y),(x+w,y+h),(255,255,255),-1)
使用 HSV 颜色 space 非常适合找出特定的 shade/tone 颜色。当你有主色要隔离时,你可以选择 LAB 颜色 space。我已经解释了为什么这样更好
代码:
img = cv2.imread('bill.jpg')
# create another copy for the result
img2 = img.copy()
# convert to LAB space and store b-channel
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
b_channel = lab[:,:,-1]
注意上面黄色区域的亮度。
# Perform Otsu threshold
th = cv2.threshold(b_channel, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
# Find the contour with largest area
contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
c = max(contours, key = cv2.contourArea)
# draw the contour on plain black image of same shape as original
mask = np.zeros((img.shape[0], img.shape[1]), np.uint8)
mask = cv2.drawContours(mask,[c],0,255, -1)
# dilation to avoid border effects
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
dilate = cv2.dilate(mask, kernel, iterations=1)
img2[dilate == 255] = (255, 255, 255)
另一个例子:
输入:
结果: