Python 彩色框内的作物导入
我有一个图像如下。我需要检测图像的“绿色”框并将其裁剪为单独的图像 我只能使用numpy和opencv 我看了好几篇文章,但我没能弄明白这一点。有人能帮忙吗。 根据搜索结果,我猜这需要某种面具。如果是,请提供有关如何选择特定颜色值的信息。我已经看到颜色值选择本身就是一个巨大的主题,但我自己无法理解。任何指导我都感激Python 彩色框内的作物导入,python,numpy,opencv,Python,Numpy,Opencv,我有一个图像如下。我需要检测图像的“绿色”框并将其裁剪为单独的图像 我只能使用numpy和opencv 我看了好几篇文章,但我没能弄明白这一点。有人能帮忙吗。 根据搜索结果,我猜这需要某种面具。如果是,请提供有关如何选择特定颜色值的信息。我已经看到颜色值选择本身就是一个巨大的主题,但我自己无法理解。任何指导我都感激 您可以找到所有绿色像素,找到轮廓,并裁剪找到的轮廓的边框: 凝胶图像中的所有绿色像素,其中RGB=(0,255,0): 将绿色像素转换为值为0和255的二进制图像: thresh
您可以找到所有绿色像素,找到轮廓,并裁剪找到的轮廓的边框:
- 凝胶图像中的所有绿色像素,其中RGB=(0,255,0):
- 将绿色像素转换为值为0和255的二进制图像:
thresh_gray = green_pix.astype(np.uint8)*255
- 在
中查找轮廓:thresh_gray
contours, _ = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
- 获取矩形,然后裁剪矩形
out = img[y:y+h, x:x+w, :]
import numpy as np
import cv2
# (cv_major_ver, cv_minor_ver, cv_subminor_ver) = (cv2.__version__).split('.') # Get version of OpenCV
img = cv2.imread('green_box.png')
# Gel all green pixels in the image - where RGB = (0, 255, 0)
green_pix = np.all(img == (0, 255, 0), 2)
# Convert green_pix to uint8 binary image with values 0 and 255
thresh_gray = green_pix.astype(np.uint8)*255
# Find contours in thresh_gray.
# if int(cv_major_ver) < 4:
# _, contours, _ = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# else:
# contours, _ = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2] # Shortcut (get index [-2] instead of using if-else).
# Get rectangle (assume there is only one contour)
x, y, w, h = cv2.boundingRect(contours[0])
# Crop rectangle
out = img[y:y+h, x:x+w, :]
cv2.imwrite('out.png', out) #Save out to file (for testing).
# Show result (for tesing).
cv2.imshow('out', out)
cv2.waitKey(0)
cv2.destroyAllWindows()
谢谢这很有效。如果有1个以上的绿色方框怎么办?这似乎不起作用。那我还需要做点别的吗?此外,如果输入图像是
jpg
而不是png
,是否需要更改任何内容?对于多个绿色框,可以迭代找到的所有轮廓。如果除了长方体之外,绿色像素很少,则可能需要过滤小轮廓(或使用形态学操作擦除它们)。对于jpg
,由于压缩伪影,一些绿色像素可能不会是(0,255,0)
。您可能需要查找特定范围的像素,如:红色<10,绿色>240,蓝色<10。。。
import numpy as np
import cv2
# (cv_major_ver, cv_minor_ver, cv_subminor_ver) = (cv2.__version__).split('.') # Get version of OpenCV
img = cv2.imread('green_box.png')
# Gel all green pixels in the image - where RGB = (0, 255, 0)
green_pix = np.all(img == (0, 255, 0), 2)
# Convert green_pix to uint8 binary image with values 0 and 255
thresh_gray = green_pix.astype(np.uint8)*255
# Find contours in thresh_gray.
# if int(cv_major_ver) < 4:
# _, contours, _ = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# else:
# contours, _ = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2] # Shortcut (get index [-2] instead of using if-else).
# Get rectangle (assume there is only one contour)
x, y, w, h = cv2.boundingRect(contours[0])
# Crop rectangle
out = img[y:y+h, x:x+w, :]
cv2.imwrite('out.png', out) #Save out to file (for testing).
# Show result (for tesing).
cv2.imshow('out', out)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Find indices of green pixels.
idx = np.where(np.all(img == (0, 255, 0), 2))
# Get minimum and maximum index in both axes (top left corner and bottom right corner)
x0, y0, x1, y1 = idx[1].min(), idx[0].min(), idx[1].max(), idx[0].max()
# Crop rectangle
out = img[y0:y1+1, x0:x1+1, :]