裁剪图像opencv python

问题描述 投票:0回答:3

请帮我裁剪这张图片。 我需要将下面的图像分割成两个图像来比较它们,但我想以这样的方式剪切它们,使两个裁剪后的图像具有相同的大小,并删除两个图像之间的多余区域。

我用了下面的代码来裁剪,但是好像效果不是很好。

import cv2
import numpy as np

# Đọc bức ảnh gốc
image = cv2.imread('2.jpg')

# Lấy chiều cao và chiều rộng của ảnh
height, width = image.shape[:2]

# Tính vị trí cắt giữa hai ảnh
middle = width // 2

# Cắt và lấy hai phần bức ảnh
image1 = image[:, :middle]
image2 = image[:, middle:]

# Tìm contours trong ảnh 1
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
_, thresh1 = cv2.threshold(gray1, 1, 255, cv2.THRESH_BINARY)
contours1, _ = cv2.findContours(thresh1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x1, _, w1, _ = cv2.boundingRect(contours1[0])

# Tìm contours trong ảnh 2
gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
_, thresh2 = cv2.threshold(gray2, 1, 255, cv2.THRESH_BINARY)
contours2, _ = cv2.findContours(thresh2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x2, _, w2, _ = cv2.boundingRect(contours2[0])

# Cắt bỏ vùng đen giữa hai ảnh
image1 = image1[:, x1:(x1+w1)]
image2 = image2[:, x2:(x2+w2)]

# Hiển thị hai ảnh đã cắt
cv2.imshow('Image 1', image1)
cv2.imshow('Image 2', image2)
cv2.waitKey(0)
cv2.destroyAllWindows()

请帮我编写代码以根据要求裁剪图像。

python image opencv image-processing crop
3个回答
1
投票

这是使用 Python/OpenCV 的替代方法。

  • 读取输入内容
  • 黑色阈值
  • 应用形态学打开来删除薄区域
  • 获取每列的非零像素数
  • 找到计数大于某个阈值 (99%) 的第一个(最小)位置
  • 找到计数大于某个阈值 (99%) 的最后(最大)位置
  • 使用这些值将图像裁剪为两部分
  • 保存结果

输入:

import cv2
import numpy as np

# read the image
img = cv2.imread('tom_and_jerry.jpg')
hh, ww = img.shape[:2]

# threshold on black
lower = (0,0,0)
upper = (10,10,10)
thresh = cv2.inRange(img, lower, upper)

# apply morphology open to remove thin lines
kernel = np.ones((3,3), np.uint8)
morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

# count the number of non-zero pixels in each column
counts = np.count_nonzero(morph, axis=0)

# get the first and last position where counts >= 99% of hh
T=0.99
min = np.amin(np.where(counts>=T*hh))
max = np.amax(np.where(counts>=T*hh))
print(min,max)

# crop start x and width w
x = min-1
w = max-min+2


# crop the image into two parts
crop1 = img[0:hh, 0:x]
crop2 = img[0:hh, x+w:ww]

# save results
cv2.imwrite('tom_and_jerry_thresh.jpg', thresh)
cv2.imwrite('tom_and_jerry_morph.jpg', morph)
cv2.imwrite('tom_and_jerry_crop1.jpg', crop1)
cv2.imwrite('tom_and_jerry_crop2.jpg', crop2)

# show results
cv2.imshow('thresh', thresh)
cv2.imshow('morph', morph)
cv2.imshow('crop1', crop1)
cv2.imshow('crop2', crop2)
cv2.waitKey(0)

作物 1:

作物 2:


1
投票

这是在 Python/OpenCV 中分离这两部分的一种方法

  • 读取输入内容
  • 黑色阈值
  • 应用形态打开以去除细线
  • 获取最大的外部轮廓
  • 获取边界框
  • 使用输入和边界框的大小来裁剪两个部分
  • 保存结果

输入:

import cv2
import numpy as np

# read the image
img = cv2.imread('tom_and_jerry.jpg')
hh, ww = img.shape[:2]

# threshold on black
lower = (0,0,0)
upper = (10,10,10)
thresh = cv2.inRange(img, lower, upper)

# apply morphology open to remove thin lines
kernel = np.ones((3,3), np.uint8)
morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

# get largest contour
contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

# get bounding box
x,y,w,h = cv2.boundingRect(big_contour)

# crop the image into two parts
crop1 = img[0:hh, 0:x]
crop2 = img[0:hh, x+w:ww]

# save results
cv2.imwrite('tom_and_jerry_thresh.jpg', thresh)
cv2.imwrite('tom_and_jerry_morph.jpg', morph)
cv2.imwrite('tom_and_jerry_crop1.jpg', crop1)
cv2.imwrite('tom_and_jerry_crop2.jpg', crop2)

# show results
cv2.imshow('thresh', thresh)
cv2.imshow('morph', morph)
cv2.imshow('crop1', crop1)
cv2.imshow('crop2', crop2)
cv2.waitKey(0)

阈值图像:

形态学清理图像:

作物 1:

作物 2:


0
投票

我也试图找到像你这样的两个相似图像之间的差异,但似乎我的方法不合适。您比较过这两张图片吗?如果是这样,请指导我如何去做。非常感谢。

© www.soinside.com 2019 - 2024. All rights reserved.