我试图找到手绘线的两个端点,我已经编写了这个找到轮廓的片段,但端点不正确:
img = cv2.imread("my_img.jpeg")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Binary Threshold:
_, thr_img = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
cv2.imshow(winname="after threshold", mat=thr_img)
cv2.waitKey(0)
contours, _ = cv2.findContours(image=thr_img, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)
for idx, cnt in enumerate(contours):
print("Contour #", idx)
cv2.drawContours(image=img, contours=[cnt], contourIdx=0, color=(255, 0, 0), thickness=3)
cv2.circle(img, tuple(cnt[0][0]), 5, (255, 255, 0), 5) # Result in wrong result
cv2.circle(img, tuple(cnt[-1][0]), 5, (0, 0, 255), 5) # Result in wrong result
cv2.imshow(winname="contour" + str(idx), mat=img)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
原图:
我也尝试过cornerHarris,但它给了我一些加分,
有人可以建议一个准确且更好的方法吗?
我有一个骨架图像(如下所示)。
我想得到线条的交点。我在下面尝试了以下方法,skeleton是一个 openCV 图像,算法返回一个坐标列表:
def getSkeletonIntersection(skeleton):
image = skeleton.copy();
image = image/255;
intersections = list();
for y in range(1,len(image)-1):
for x in range(1,len(image[y])-1):
if image[y][x] == 1:
neighbourCount = 0;
neighbours = neighbourCoords(x,y);
for n in neighbours:
if (image[n[1]][n[0]] == 1):
neighbourCount += 1;
if(neighbourCount > 2):
print(neighbourCount,x,y);
intersections.append((x,y));
return intersections;
Run Code Online (Sandbox Code Playgroud)
它找到有两个以上相邻像素的白色像素的坐标。我认为这只会返回角落,但事实并非如此 - 它会返回更多点。
这是其检测到的点标记在图像上的输出。这是因为它检测到下面显示的一些不是相交的示例。
0 0 0 1 1 0 0 1 1
1 1 1 0 1 0 1 1 0
0 0 1 0 0 …Run Code Online (Sandbox Code Playgroud) python opencv image-processing line-intersection opencv-contour