简体   繁体   English

在 python 中使用 opencv 剪切图像的特定部分

[英]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 中的一种方法。

  • Read the image阅读图片
  • Read the mask (separately created one time from your other image)阅读面具(与您的另一张图片分开创建一次)
  • Convert the mask to gray and threshold it to binary, invert it and make it 3 channels将蒙版转换为灰色并将其阈值化为二进制,将其反转并使其成为 3 个通道
  • Get the center of the circle from your own code.从您自己的代码中获取圆心。 (I have just measured it manually) (我只是手动测量的)
  • Set the expected x,y offsets of the bottom of the region of text from the center of the circle设置文本区域底部距圆心的预期 x,y 偏移量
  • Compute the expected top left corner of the mask from the center of the circle, the offsets and the height of the mask image从圆心、偏移量和蒙版图像的高度计算蒙版的预期左上角
  • Put the mask into black image the size of the input at that location将掩码放入黑色图像中,该位置的输入大小
  • Apply the new mask to the image to make the rest of the image black将新蒙版应用于图像,使图像的 rest 变为黑色
  • Crop out the region of interest from the top left corner and the size of the original mask从左上角裁剪出感兴趣区域和原始蒙版的大小
  • OPTIONALLY, crop the original image可选地,裁剪原始图像
  • Save the results保存结果

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.

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