[英]cut out a specific part of an image with opencv in python
I have an image of an IC die and I want to cut out the marking in the center.The marking is always at this specific position above the circle at the bottom left.我有一张 IC 芯片的图像,我想剪掉中心的标记。标记始终位于左下角圆圈上方的特定 position 上。 The idea is to first find the circle position which I already accomplished with the hough circle transformation.
这个想法是首先找到我已经通过霍夫圆变换完成的圆 position。 Now I want to cut out the part where the marking is.
现在我想剪掉标记所在的部分。 It should ideally be a not a square or rectangle but something more like in the image:
理想情况下,它应该不是正方形或矩形,而是更像图像中的东西:
This is a part of my code:这是我的代码的一部分:
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(morph_image, cv2.HOUGH_GRADIENT, 1.3, 20, param1=50, param2=25, minRadius=15,
maxRadius=19)
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
# Zeichne äußeren Kreis
cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2)
# Zeichne Kreiszentrum
cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3)
# Tupel mit x- und y-Koordinaten des Kreiszentrums
circle_center = (i[0], i[1])
print('Die Koordinaten des Kreiszentrums lauten: ', circle_center)
"""cv2.imshow('Kreis', cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()"""
else:
circle_center = None
print('Kein Kreis gefunden')
"""cv2.imshow('Kein Kreis', cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()"""
so my cicle center
has the center position of my circle (eg (124, 370)
).所以我的
cicle center
有我的圆圈的中心 position (例如(124, 370)
)。 How can I cut out this part of the image automatically?如何自动剪切图像的这一部分? Can I somehow crop it out?
我可以以某种方式将其裁剪掉吗? Ideally I would want to crop the marking out into another image to inspect it separately but the normal cropping approach with
marking_img = img[y:y+h, x:x+w]
wouldn't work I guess.理想情况下,我想将标记裁剪到另一个图像中以单独检查它,但我猜使用
marking_img = img[y:y+h, x:x+w]
的正常裁剪方法行不通。
EDIT: Here is the original image:编辑:这是原始图像:
The output should be like the first image and if it is possible something like this: output 应该像第一张图片,如果可能的话,如下所示:
So in the end I would want to have 2 images: One image with just the die without the marking and one image with just the marking所以最后我想要两张图像:一张只有模具没有标记的图像和一张只有标记的图像
Here is one way in Python/OpenCV.这是 Python/OpenCV 中的一种方法。
Input image:输入图像:
Prepared mask image:准备好的掩码图像:
import cv2
import numpy as np
# read image
img = cv2.imread('die.jpg')
ht, wd, cc = img.shape
# read mask as grayscale
mask = cv2.imread('die_mask.png', cv2.IMREAD_GRAYSCALE)
# threshold mask and invert
mask = cv2.threshold(mask,0,255,cv2.THRESH_BINARY)[1]
mask = 255 - mask
hh, ww = mask.shape
# make mask 3 channel
mask = cv2.merge([mask,mask,mask])
# set circle center
cx = 62
cy = 336
# offsets from circle center to bottom of region
dx = -20
dy = -27
# compute top left corner of mask using size of mask and center and offsets
left = cx + dx
top = cy + dy - hh
# put mask into black background image
mask2 = np.zeros_like(img)
mask2[top:top+hh, left:left+ww] = mask
# apply mask to image
img_masked = cv2.bitwise_and(img, mask2)
# crop region
img_masked_cropped = img_masked[top:top+hh, left:left+ww]
# ALTERNATE just crop input
img_cropped = img[top:top+hh, left:left+ww]
cv2.imshow('image', img)
cv2.imshow('mask', mask)
cv2.imshow('mask2', mask2)
cv2.imshow('masked image', img_masked)
cv2.imshow('masked cropped image', img_masked_cropped)
cv2.imshow('cropped image', img_cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save results
cv2.imwrite('die_mask_inserted.jpg', mask2)
cv2.imwrite('die_masked_image.jpg', img_masked)
cv2.imwrite('die_masked_cropped.jpg', img_masked_cropped)
cv2.imwrite('die_cropped.jpg', img_cropped)
Mask inserted in black image:插入黑色图像的蒙版:
Masked image:蒙面图像:
Crop of masked image:蒙版图像的裁剪:
(Optional) Crop of input image: (可选)输入图像的裁剪:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.