繁体   English   中英

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

[英]cut out a specific part of an image with opencv in python

我有一张 IC 芯片的图像,我想剪掉中心的标记。标记始终位于左下角圆圈上方的特定 position 上。 这个想法是首先找到我已经通过霍夫圆变换完成的圆 position。 现在我想剪掉标记所在的部分。 理想情况下,它应该不是正方形或矩形,而是更像图像中的东西:

例子

这是我的代码的一部分:

        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()"""

所以我的cicle center有我的圆圈的中心 position (例如(124, 370) )。 如何自动剪切图像的这一部分? 我可以以某种方式将其裁剪掉吗? 理想情况下,我想将标记裁剪到另一个图像中以单独检查它,但我猜使用marking_img = img[y:y+h, x:x+w]的正常裁剪方法行不通。

编辑:这是原始图像:

在此处输入图像描述

output 应该像第一张图片,如果可能的话,如下所示:

在此处输入图像描述

所以最后我想要两张图像:一张只有模具没有标记的图像和一张只有标记的图像

这是 Python/OpenCV 中的一种方法。

  • 阅读图片
  • 阅读面具(与您的另一张图片分开创建一次)
  • 将蒙版转换为灰色并将其阈值化为二进制,将其反转并使其成为 3 个通道
  • 从您自己的代码中获取圆心。 (我只是手动测量的)
  • 设置文本区域底部距圆心的预期 x,y 偏移量
  • 从圆心、偏移量和蒙版图像的高度计算蒙版的预期左上角
  • 将掩码放入黑色图像中,该位置的输入大小
  • 将新蒙版应用于图像,使图像的 rest 变为黑色
  • 从左上角裁剪出感兴趣区域和原始蒙版的大小
  • 可选地,裁剪原始图像
  • 保存结果

输入图像:

在此处输入图像描述

准备好的掩码图像:

在此处输入图像描述

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)


插入黑色图像的蒙版:

在此处输入图像描述

蒙面图像:

在此处输入图像描述

蒙版图像的裁剪:

在此处输入图像描述

(可选)输入图像的裁剪:

在此处输入图像描述

暂无
暂无

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

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