Crop image opencv python

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

Crop image opencv python

问题

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

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

  1. import cv2
  2. import numpy as np
  3. # 读取原始图像
  4. image = cv2.imread('2.jpg')
  5. # 获取图像的高度和宽度
  6. height, width = image.shape[:2]
  7. # 计算两个图像之间的切割位置
  8. middle = width // 2
  9. # 裁剪并获取两个部分的图像
  10. image1 = image[:, :middle]
  11. image2 = image[:, middle:]
  12. # 在图像1中查找轮廓
  13. gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
  14. _, thresh1 = cv2.threshold(gray1, 1, 255, cv2.THRESH_BINARY)
  15. contours1, _ = cv2.findContours(thresh1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  16. x1, _, w1, _ = cv2.boundingRect(contours1[0])
  17. # 在图像2中查找轮廓
  18. gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
  19. _, thresh2 = cv2.threshold(gray2, 1, 255, cv2.THRESH_BINARY)
  20. contours2, _ = cv2.findContours(thresh2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  21. x2, _, w2, _ = cv2.boundingRect(contours2[0])
  22. # 剪切掉两个图像之间的黑色区域
  23. image1 = image1[:, x1:(x1+w1)]
  24. image2 = image2[:, x2:(x2+w2)]
  25. # 显示裁剪后的两张图像
  26. cv2.imshow('Image 1', image1)
  27. cv2.imshow('Image 2', image2)
  28. cv2.waitKey(0)
  29. 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.

  1. import cv2
  2. import numpy as np
  3. # Đọc bức ảnh gốc
  4. image = cv2.imread('2.jpg')
  5. # Lấy chiều cao và chiều rộng của ảnh
  6. height, width = image.shape[:2]
  7. # Tính vị trí cắt giữa hai ảnh
  8. middle = width // 2
  9. # Cắt và lấy hai phần bức ảnh
  10. image1 = image[:, :middle]
  11. image2 = image[:, middle:]
  12. # Tìm contours trong ảnh 1
  13. gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
  14. _, thresh1 = cv2.threshold(gray1, 1, 255, cv2.THRESH_BINARY)
  15. contours1, _ = cv2.findContours(thresh1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  16. x1, _, w1, _ = cv2.boundingRect(contours1[0])
  17. # Tìm contours trong ảnh 2
  18. gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
  19. _, thresh2 = cv2.threshold(gray2, 1, 255, cv2.THRESH_BINARY)
  20. contours2, _ = cv2.findContours(thresh2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  21. x2, _, w2, _ = cv2.boundingRect(contours2[0])
  22. # Cắt bỏ vùng đen giữa hai ảnh
  23. image1 = image1[:, x1:(x1+w1)]
  24. image2 = image2[:, x2:(x2+w2)]
  25. # Hiển thị hai ảnh đã cắt
  26. cv2.imshow('Image 1', image1)
  27. cv2.imshow('Image 2', image2)
  28. cv2.waitKey(0)
  29. cv2.destroyAllWindows()

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

答案1

得分: 1

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

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

输入:

  1. import cv2
  2. import numpy as np
  3. # 读取图像
  4. img = cv2.imread('tom_and_jerry.jpg')
  5. hh, ww = img.shape[:2]
  6. # 在黑色上进行阈值处理
  7. lower = (0,0,0)
  8. upper = (10,10,10)
  9. thresh = cv2.inRange(img, lower, upper)
  10. # 应用形态学开运算以去除细线
  11. kernel = np.ones((3,3), np.uint8)
  12. morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
  13. # 获取最大轮廓
  14. contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  15. contours = contours[0] if len(contours) == 2 else contours[1]
  16. big_contour = max(contours, key=cv2.contourArea)
  17. # 获取边界框
  18. x,y,w,h = cv2.boundingRect(big_contour)
  19. # 将图像裁剪成两个部分
  20. crop1 = img[0:hh, 0:x]
  21. crop2 = img[0:hh, x+w:ww]
  22. # 保存结果
  23. cv2.imwrite('tom_and_jerry_thresh.jpg', thresh)
  24. cv2.imwrite('tom_and_jerry_morph.jpg', morph)
  25. cv2.imwrite('tom_and_jerry_crop1.jpg', crop1)
  26. cv2.imwrite('tom_and_jerry_crop2.jpg', crop2)
  27. # 显示结果
  28. cv2.imshow('thresh', thresh)
  29. cv2.imshow('morph', morph)
  30. cv2.imshow('crop1', crop1)
  31. cv2.imshow('crop2', crop2)
  32. 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

  1. import cv2
  2. import numpy as np
  3. # read the image
  4. img = cv2.imread('tom_and_jerry.jpg')
  5. hh, ww = img.shape[:2]
  6. # threshold on black
  7. lower = (0,0,0)
  8. upper = (10,10,10)
  9. thresh = cv2.inRange(img, lower, upper)
  10. # apply morphology open to remove thin lines
  11. kernel = np.ones((3,3), np.uint8)
  12. morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
  13. # get largest contour
  14. contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  15. contours = contours[0] if len(contours) == 2 else contours[1]
  16. big_contour = max(contours, key=cv2.contourArea)
  17. # get bounding box
  18. x,y,w,h = cv2.boundingRect(big_contour)
  19. # crop the image into two parts
  20. crop1 = img[0:hh, 0:x]
  21. crop2 = img[0:hh, x+w:ww]
  22. # save results
  23. cv2.imwrite('tom_and_jerry_thresh.jpg', thresh)
  24. cv2.imwrite('tom_and_jerry_morph.jpg', morph)
  25. cv2.imwrite('tom_and_jerry_crop1.jpg', crop1)
  26. cv2.imwrite('tom_and_jerry_crop2.jpg', crop2)
  27. # show results
  28. cv2.imshow('thresh', thresh)
  29. cv2.imshow('morph', morph)
  30. cv2.imshow('crop1', crop1)
  31. cv2.imshow('crop2', crop2)
  32. 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%)
  • 使用这些值来裁剪图像为两部分
  • 保存结果

输入:

  1. import cv2
  2. import numpy as np
  3. # 读取图像
  4. img = cv2.imread('tom_and_jerry.jpg')
  5. hh, ww = img.shape[:2]
  6. # 基于黑色进行阈值处理
  7. lower = (0, 0, 0)
  8. upper = (10, 10, 10)
  9. thresh = cv2.inRange(img, lower, upper)
  10. # 应用形态学开操作以去除细线
  11. kernel = np.ones((3, 3), np.uint8)
  12. morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
  13. # 计算每列中非零像素的数量
  14. counts = np.count_nonzero(morph, axis=0)
  15. # 获取计数大于等于hh的99%的第一个和最后一个位置
  16. T = 0.99
  17. min = np.amin(np.where(counts >= T * hh))
  18. max = np.amax(np.where(counts >= T * hh))
  19. print(min, max)
  20. # 裁剪开始的x位置和宽度w
  21. x = min - 1
  22. w = max - min + 2
  23. # 将图像裁剪成两部分
  24. crop1 = img[0:hh, 0:x]
  25. crop2 = img[0:hh, x + w:ww]
  26. # 保存结果
  27. cv2.imwrite('tom_and_jerry_thresh.jpg', thresh)
  28. cv2.imwrite('tom_and_jerry_morph.jpg', morph)
  29. cv2.imwrite('tom_and_jerry_crop1.jpg', crop1)
  30. cv2.imwrite('tom_and_jerry_crop2.jpg', crop2)
  31. # 显示结果
  32. cv2.imshow('thresh', thresh)
  33. cv2.imshow('morph', morph)
  34. cv2.imshow('crop1', crop1)
  35. cv2.imshow('crop2', crop2)
  36. 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

  1. import cv2
  2. import numpy as np
  3. # read the image
  4. img = cv2.imread('tom_and_jerry.jpg')
  5. hh, ww = img.shape[:2]
  6. # threshold on black
  7. lower = (0,0,0)
  8. upper = (10,10,10)
  9. thresh = cv2.inRange(img, lower, upper)
  10. # apply morphology open to remove thin lines
  11. kernel = np.ones((3,3), np.uint8)
  12. morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
  13. # count the number of non-zero pixels in each column
  14. counts = np.count_nonzero(morph, axis=0)
  15. # get the first and last position where counts >= 99% of hh
  16. T=0.99
  17. min = np.amin(np.where(counts>=T*hh))
  18. max = np.amax(np.where(counts>=T*hh))
  19. print(min,max)
  20. # crop start x and width w
  21. x = min-1
  22. w = max-min+2
  23. # crop the image into two parts
  24. crop1 = img[0:hh, 0:x]
  25. crop2 = img[0:hh, x+w:ww]
  26. # save results
  27. cv2.imwrite('tom_and_jerry_thresh.jpg', thresh)
  28. cv2.imwrite('tom_and_jerry_morph.jpg', morph)
  29. cv2.imwrite('tom_and_jerry_crop1.jpg', crop1)
  30. cv2.imwrite('tom_and_jerry_crop2.jpg', crop2)
  31. # show results
  32. cv2.imshow('thresh', thresh)
  33. cv2.imshow('morph', morph)
  34. cv2.imshow('crop1', crop1)
  35. cv2.imshow('crop2', crop2)
  36. 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:

确定