【发布时间】:2020-08-25 13:44:47
【问题描述】:
我想检测enter image description hereimage中的所有补丁,我附上了用于检测它们的代码:
import cv2
import numpy as np
import matplotlib.pyplot as plt
image=cv2.imread("bw2.jpg",0)
# convert to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# create a binary thresholded image
_, binary = cv2.threshold(gray, 0, 500, cv2.THRESH_BINARY_INV)
# show it
plt.imshow(gray, cmap="gray")
plt.show()
# find the contours from the thresholded image
contours, hierarchy = cv2.findContours(gray, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
print("contours:",contours)
# draw all contours
for c in contours:
if cv2.contourArea(c) < 3000:
continue
(x, y, w, h) = cv2.boundingRect(c)
#cv2.rectangle(image, (x,y), (x+w,y+h), (0, 255, 0), 2)
## BEGIN - draw rotated rectangle
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(image,[box],0,(255,51,255),2)
# show the image with the drawn contours
plt.imshow(image)
#plt.imshow(im3)
cv2.imwrite("detectImg2.png",image)
plt.show()
我在这里得到输出图像enter image description here
我想检测所有这些,谁能告诉我如何实现这一点,我是图像处理的新手
【问题讨论】:
-
您无法裁剪旋转的矩形。您可以使旋转矩形外的背景透明,也可以裁剪到旋转矩形的正常边界框。请提供更多详细信息您真正想要什么或显示示例输出。请参加 tour (stackoverflow.com/tour) 并阅读帮助中心 (stackoverflow.com/help) 中关于“如何提出一个好问题”(@987654325) 的指南@) 和“如何创建最小的、可重现的示例”(stackoverflow.com/help/minimal-reproducible-example)。
-
我其实是想把补丁剪下来直接旋转,希望大家理解
-
您需要裁剪到旋转矩形的边界,使背景变为白色,然后旋转以拉直它,然后再次裁剪.. cv2.minAreaRect() 将返回您需要使用的旋转角度旋转。然后使用轮廓通过在旋转时将背景设为白色来找到边界并获得轮廓边界框。或者获取旋转的矩形角并相应地旋转它们,并使用旋转的角从角的边界框中裁剪图像。请阅读本论坛中的导览,并尽可能发布您当前的代码。
-
如你所说,我把所有信息都放在这里了,我做到了一半,但仍然缺少图像中的一些补丁。
-
我不明白你想要什么。您是否试图分别获得白色簇或每个白色区域的旋转矩形。我认为是后者,但想确定我理解。我是否理解您没有检测到所有这些?也许您的区域阈值太小?
标签: python opencv image-processing