Crop image opencv python

huangapple go评论70阅读模式
英文:

Crop image opencv python

问题

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

我已经使用以下代码进行裁剪,但似乎效果不太好。

import cv2
import numpy as np

# 读取原始图像
image = cv2.imread('2.jpg')

# 获取图像的高度和宽度
height, width = image.shape[:2]

# 计算两个图像之间的切割位置
middle = width // 2

# 裁剪并获取两个部分的图像
image1 = image[:, :middle]
image2 = image[:, middle:]

# 在图像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])

# 在图像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])

# 剪切掉两个图像之间的黑色区域
image1 = image1[:, x1:(x1+w1)]
image2 = image2[:, x2:(x2+w2)]

# 显示裁剪后的两张图像
cv2.imshow('Image 1', image1)
cv2.imshow('Image 2', image2)
cv2.waitKey(0)
cv2.destroyAllWindows()

请帮助我编写能够按照要求裁剪图像的代码。

英文:

Please help me crop this image.
I need to split the image below into two images to compare them, but I want to cut them in such a way that the two cropped images have the same size and remove the excess area between the two images.
Crop image opencv python

I have used the following code to crop, but it seems to be not very effective.

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

Please help me with the code to crop the image as requested.

答案1

得分: 1

以下是在Python/OpenCV中分离两个部分的一种方法:

  • 读取输入
  • 在黑色上进行阈值处理
  • 应用形态学开运算以去除细线
  • 获取最大的外部轮廓
  • 获取边界框
  • 使用输入和边界框的尺寸来裁剪两个部分
  • 保存结果

输入:

import cv2
import numpy as np

# 读取图像
img = cv2.imread('tom_and_jerry.jpg')
hh, ww = img.shape[:2]

# 在黑色上进行阈值处理
lower = (0,0,0)
upper = (10,10,10)
thresh = cv2.inRange(img, lower, upper)

# 应用形态学开运算以去除细线
kernel = np.ones((3,3), np.uint8)
morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

# 获取最大轮廓
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)

# 获取边界框
x,y,w,h = cv2.boundingRect(big_contour)

# 将图像裁剪成两个部分
crop1 = img[0:hh, 0:x]
crop2 = img[0:hh, x+w:ww]

# 保存结果
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)

# 显示结果
cv2.imshow('thresh', thresh)
cv2.imshow('morph', morph)
cv2.imshow('crop1', crop1)
cv2.imshow('crop2', crop2)
cv2.waitKey(0)

阈值处理后的图像:

Crop image opencv python

经过形态学处理后的图像:

Crop image opencv python

裁剪1:

Crop image opencv python

裁剪2:

Crop image opencv python

英文:

Here is one way to separate the two parts in Python/OpenCV

  • Read the input
  • Threshold on black
  • Apply morphology open to remove thin lines
  • Get the largest external contour
  • Get the bounding box
  • Use the size of the input and bounding box to crop the two parts
  • Save the results

Input:

Crop image opencv python

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)

Threshold Image:

Crop image opencv python

Morphology Cleaned Image:

Crop image opencv python

Crop 1:

Crop image opencv python

Crop 2:

Crop image opencv python

答案2

得分: 1

这是使用Python/OpenCV的另一种方法。

  • 读取输入
  • 基于黑色进行阈值处理
  • 应用形态学开操作以去除细小区域
  • 获取沿每列的非零像素计数
  • 找到第一个(最小)位置,其中计数大于某个阈值(99%)
  • 找到最后一个(最大)位置,其中计数大于某个阈值(99%)
  • 使用这些值来裁剪图像为两部分
  • 保存结果

输入:

import cv2
import numpy as np

# 读取图像
img = cv2.imread('tom_and_jerry.jpg')
hh, ww = img.shape[:2]

# 基于黑色进行阈值处理
lower = (0, 0, 0)
upper = (10, 10, 10)
thresh = cv2.inRange(img, lower, upper)

# 应用形态学开操作以去除细线
kernel = np.ones((3, 3), np.uint8)
morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

# 计算每列中非零像素的数量
counts = np.count_nonzero(morph, axis=0)

# 获取计数大于等于hh的99%的第一个和最后一个位置
T = 0.99
min = np.amin(np.where(counts >= T * hh))
max = np.amax(np.where(counts >= T * hh))
print(min, max)

# 裁剪开始的x位置和宽度w
x = min - 1
w = max - min + 2

# 将图像裁剪成两部分
crop1 = img[0:hh, 0:x]
crop2 = img[0:hh, x + w:ww]

# 保存结果
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)

# 显示结果
cv2.imshow('thresh', thresh)
cv2.imshow('morph', morph)
cv2.imshow('crop1', crop1)
cv2.imshow('crop2', crop2)
cv2.waitKey(0)

裁剪1:

Crop image opencv python

裁剪2:

Crop image opencv python

英文:

Here is an alternate approach using Python/OpenCV.

  • Read the input
  • Threshold on black
  • Apply morphology open to remove thin regions
  • Get the counts of non-zero pixels along each column
  • Find the first (min) location where the counts are greater than some threshold (99%)
  • Find the last (max) location where the counts are greater than some threshold (99%)
  • Use those values to crop the image into the two parts
  • Save the results

Input:

Crop image opencv python

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)

Crop 1:

Crop image opencv python

Crop 2:

Crop image opencv python

huangapple
  • 本文由 发表于 2023年7月4日 23:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定