简体   繁体   English

使用openCV查找图片中的极值点

[英]Finding the extreme points in picture using openCV

The challenge: I want to use the edges highlighted by the green dots to crop the image.挑战:我想使用绿点突出显示的边缘来裁剪图像。 I need to locate the points, and then use that to crop.我需要找到这些点,然后用它来裁剪。

What I've done: I've tried to use threshold to get to the point where I eliminate the rest of the picture and only highlight the corner points and it works.我所做的:我尝试使用阈值来消除图片的其余部分并仅突出显示角点并且它有效。 Only issue is that I can't find a way to highlight the edge (the ones in green in the second picture) as the outermost pixel.唯一的问题是我找不到将边缘(第二张图片中的绿色边缘)突出显示为最外层像素的方法。 All the techniques I'm reading up on use contours, but because these become independent dots, connected components seems better in theory.我正在阅读的所有关于使用轮廓的技术,但是因为这些成为独立的点,连接组件在理论上似乎更好。 I am however stumped how to use connected components for this task.然而,我很难过如何使用连接的组件来完成这项任务。

Here is the original picture:这是原始图片: 在此处输入图像描述

Here is the picture with the green dots illustrating the corners of interest:这是带有绿点的图片,说明了感兴趣的角落: 在此处输入图像描述

Here is the picture after threshold:这是阈值后的图片: 在此处输入图像描述

Clearly contouring won't work.显然轮廓是行不通的。 Is there a better way to approach this problem?有没有更好的方法来解决这个问题?

Yes, using morphology is a good idea.是的,使用形态学是个好主意。 I will supplement Basj's answer with the code and the result:我将用代码和结果补充Basj 的答案:

import cv2
img = cv2.imread("XfRte.jpg", cv2.IMREAD_GRAYSCALE)
ret, thresh = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY_INV)
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(7,7))
img_open = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
coord=cv2.boundingRect(img_open)
img_crop=img[coord[1]:coord[3]+coord[1], coord[0]:coord[2]+coord[0]]
cv2.imwrite('ans.png', img_crop)

结果

Since the symbols you are trying to recognize are the thickest symbols on this picture, you can use an "Erosion" followed by a "Dilation".由于您要识别的符号是这张图片上最粗的符号,您可以使用“侵蚀”,然后使用“膨胀”。 This is called an "Opening".这被称为“打开”。

Do this on a copy img2 of your image img , after these steps you will have only the 4 symbols you're looking for.在您的图像img的副本img2上执行此操作,在这些步骤之后,您将只有您正在寻找的 4 个符号。 Then you easily find the coordinates.然后你很容易找到坐标。 Based on the coordinates, you can then crop your original image img .然后,您可以根据坐标裁剪原始图像img

All this is available directly in Python OpenCV.所有这些都可以直接在 Python OpenCV 中使用。

If the colors are inversed, you might try the same operation in the reverse order.如果颜色相反,您可以按相反的顺序尝试相同的操作。

See Morphological Transformations .请参阅形态变换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM