我使用OpenCV和Python编写了以下代码:
import cv2
cap = cv2.VideoCapture(1)
cv2.namedWindow('Original')
cv2.namedWindow('Captured')
cv2.namedWindow('Deffects')
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('c'):
cv2.imshow('Captured', gray)
cv2.imwrite('tswira.jpg', frame)
if cv2.waitKey(1) == ord('s'):
img1 = cv2.imread('carte1.jpg', 0)
img2 = cv2.imread('tswira.JPG', 0)
img1 = cv2.resize(img1, (250, 250))
img2 = cv2.resize(img2, (250, 250))
sub = img1 - img2
cv2.imshow('Original', img1)
cv2.imshow('Captured', img2)
cv2.imshow('Deffects', sub)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
这是我作为输出得到的图像:
然而,我的问题是:我如何才能只修剪白色区域
在Python/OpenCV中有一种方法可以做到这一点
输入:
阈值图像:
形态闭合掩模:
输入时绘制的轮廓:
裁剪图像:
这样就可以了:
首先,读取图像,将其转换为灰度,并强制将右侧和底部的外部条纹变为黑色
现在对图像进行阈值设置,找到白点的坐标,并获取白点的最小和最大x和y坐标
现在,您可以裁剪、写入和显示图像:
以下是裁剪区域:
相关问题 更多 >
编程相关推荐