1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python检测图像中的矩形_检测图像中的矩形并裁剪

python检测图像中的矩形_检测图像中的矩形并裁剪

时间:2020-10-10 16:18:17

相关推荐

python检测图像中的矩形_检测图像中的矩形并裁剪

如果你努力的话,那是件容易的事。这是我的输出(图像和它的一小部分)

我做了什么?先调整图像的大小,因为它在我的屏幕上太大了

侵蚀、扩张以去除小点并加厚线条

阈值图像

洪水泛滥,从正确的点开始

倒洪

找到轮廓并一次画一个,其范围约为

矩形上的区域。对于我调整大小的(500x500)图像,我将

轮廓在500到2500之间(无论如何都是反复试验)。

找到边界矩形并从主图像中裁剪该遮罩。

然后用正确的名字保存那篇文章-我没有这么做。

也许,有一个更简单的方法,但我喜欢这个。不放代码是因为

我弄得很笨拙。如果你还需要的话会放进去的。

下面是每次找到轮廓时遮罩的外观

代码:import cv2;

import numpy as np;

# Run the code with the image name, keep pressing space bar

# Change the kernel, iterations, Contour Area, position accordingly

# These values work for your present image

img = cv2.imread("your_image.jpg", 0);

h, w = img.shape[:2]

kernel = np.ones((15,15),np.uint8)

e = cv2.erode(img,kernel,iterations = 2)

d = cv2.dilate(e,kernel,iterations = 1)

ret, th = cv2.threshold(d, 150, 255, cv2.THRESH_BINARY_INV)

mask = np.zeros((h+2, w+2), np.uint8)

cv2.floodFill(th, mask, (200,200), 255); # position = (200,200)

out = cv2.bitwise_not(th)

out= cv2.dilate(out,kernel,iterations = 3)

cnt, h = cv2.findContours(out,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for i in range(len(cnt)):

area = cv2.contourArea(cnt[i])

if(area>10000 and area<100000):

mask = np.zeros_like(img)

cv2.drawContours(mask, cnt, i, 255, -1)

x,y,w,h = cv2.boundingRect(cnt[i])

crop= img[ y:h+y,x:w+x]

cv2.imshow("snip",crop )

if(cv2.waitKey(0))==27:break

cv2.destroyAllWindows()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。