检测黑白图像上的白色矩形并裁剪 (OpenCV)
Detect white rectangle on black & white image and crop (OpenCV)
我正在努力将图像裁剪成放置在图像内部的矩形大小。这是原图:
到目前为止,我能够输入它,将其颜色转换为 HSV 颜色 space 并对其应用阈值。
到目前为止,这是我的代码:
import cv2
#Read input image
img = cv2.imread('rdm_generated_image.png')
#convert from BGR to HSV color space
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#get the saturation plane - all black/white/gray pixels are zero, and colored pixels are above zero.
s = hsv[:, :, 1]
#apply threshold on s
ret, thresh = cv2.threshold(s, 8, 255, cv2.THRESH_BINARY)
#invert colors, so every dark spots are now white
image = cv2.bitwise_not(thresh)
cv2.imwrite("image.png", image)
完成后程序输出:
现在我只想将图像裁剪到中间的大方框,但无法检测到它的轮廓。
我用cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
和其他一些函数试过了,但我还没有成功。如果这应该是错误的做法,请纠正我。感谢帮助,因为我对 OpenCV 有点陌生。
提前致谢!
这是 Python/OpenCV 中的一种方法。
- 阅读图片
- 转换为灰色
- 阈值
- 寻找轮廓
- 按大约预期区域过滤
- 绘制等高线
- 保存结果
输入:
import cv2
import numpy as np
#Read input image
img = cv2.imread('boxes.png')
#convert from BGR to HSV color space
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#apply threshold
thresh = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)[1]
# find contours and get one with area about 180*35
# draw all contours in green and accepted ones in red
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
#area_thresh = 0
min_area = 0.95*180*35
max_area = 1.05*180*35
result = img.copy()
for c in contours:
area = cv2.contourArea(c)
cv2.drawContours(result, [c], -1, (0, 255, 0), 1)
if area > min_area and area < max_area:
cv2.drawContours(result, [c], -1, (0, 0, 255), 1)
# save result
cv2.imwrite("box_found.png", result)
# show images
cv2.imshow("GRAY", gray)
cv2.imshow("THRESH", thresh)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
结果:
我正在努力将图像裁剪成放置在图像内部的矩形大小。这是原图:
到目前为止,我能够输入它,将其颜色转换为 HSV 颜色 space 并对其应用阈值。 到目前为止,这是我的代码:
import cv2
#Read input image
img = cv2.imread('rdm_generated_image.png')
#convert from BGR to HSV color space
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#get the saturation plane - all black/white/gray pixels are zero, and colored pixels are above zero.
s = hsv[:, :, 1]
#apply threshold on s
ret, thresh = cv2.threshold(s, 8, 255, cv2.THRESH_BINARY)
#invert colors, so every dark spots are now white
image = cv2.bitwise_not(thresh)
cv2.imwrite("image.png", image)
完成后程序输出:
现在我只想将图像裁剪到中间的大方框,但无法检测到它的轮廓。
我用cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
和其他一些函数试过了,但我还没有成功。如果这应该是错误的做法,请纠正我。感谢帮助,因为我对 OpenCV 有点陌生。
提前致谢!
这是 Python/OpenCV 中的一种方法。
- 阅读图片
- 转换为灰色
- 阈值
- 寻找轮廓
- 按大约预期区域过滤
- 绘制等高线
- 保存结果
输入:
import cv2
import numpy as np
#Read input image
img = cv2.imread('boxes.png')
#convert from BGR to HSV color space
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#apply threshold
thresh = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)[1]
# find contours and get one with area about 180*35
# draw all contours in green and accepted ones in red
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
#area_thresh = 0
min_area = 0.95*180*35
max_area = 1.05*180*35
result = img.copy()
for c in contours:
area = cv2.contourArea(c)
cv2.drawContours(result, [c], -1, (0, 255, 0), 1)
if area > min_area and area < max_area:
cv2.drawContours(result, [c], -1, (0, 0, 255), 1)
# save result
cv2.imwrite("box_found.png", result)
# show images
cv2.imshow("GRAY", gray)
cv2.imshow("THRESH", thresh)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
结果: