【发布时间】:2021-10-12 01:47:37
【问题描述】:
如何合并图像或其他方法,以使边界中的像素总和小于孔 x 和 y 位置。我的意思是在下面的图片中
白洞x和y的像素总和必须在孔位置更高,但如图所示,图像周围尤其是x像素的总和更高
通过这种方式,我想找到具有高值的总和像素并说明孔像素或坐标。在 y 轴上,总和当然是因为下面的结果比 x 好,但在其他一些情况下,当然,我通过下面的代码裁剪所有 1000 张图像,然后返回它们的像素数据总和和 id并说明了返回孔的最大值。
img = Image.open('J:\py.pro\path\picture_1.png').convert('L') # convert image to 8-bit grayscale
if img.mode == "CMYK":
# color profiles can be found at C:\Program Files (x86)\Common Files\Adobe\Color\Profiles\Recommended
img = ImageCms.profileToProfile(img, "USWebCoatedSWOP.icc", "sRGB_Color_Space_Profile.icm", outputMode="RGB")
# PIL image -> OpenCV image; see https://stackoverflow.com/q/14134892/2202732
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
## (1) Convert to gray, and threshold
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)
## (2) Morph-op to remove noise
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10,30))
morphed = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE, kernel)
## (3) Find the max-area contour
cnts = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
cnt = sorted(cnts, key=cv2.contourArea)[-1]
## (4) Crop and save it
x,y,w,h = cv2.boundingRect(cnt)
dst = img[y:y+h, x:x+w]
# add border/padding around the cropped image
# dst = cv2.copyMakeBorder(dst, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=[255,255,255])
#cv2.imshow("J:\\py.pro\\path\\pic_1.png", dst)
cv2.imwrite("J:\\py.pro\\path\\pic_1.png", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
【问题讨论】:
-
我的回答解决了你的问题吗?如果是这样,请考虑接受它作为您的答案 - 通过单击计票旁边的空心对勾/复选标记。如果没有,请说出什么不起作用,以便我或其他人可以进一步帮助您。谢谢。 meta.stackexchange.com/questions/5234/…
标签: image coordinates crop pixel grayscale