2017-06-26 127 views
-2

有人能告诉我如何实现图像的上下两部分?这样我可以重叠他们。例如,我有一个图像,我应该把它分开来计算每个部分的像素数量。我是OpenCV的新手,并不完全了解图像的几何形状。将图像分成两个相等的部分python opencv

+1

你能详细说说“水平分割图像的轮廓”吗? –

+1

请阅读[问] – Miki

+1

请阅读关于沟通技巧的书 – Piglet

回答

2

您可以水平向下剪切图像的顶部和底部。

打开图片。

import cv2 
import numpy as np 
image = cv2.imread('images/blobs1.png') 
cv2.imshow("Original Image", image) 
cv2.waitKey(0) 

使用image.shape让我们捕捉高度和宽度变量。

height, width = image.shape[:2] 
print image.shape 

现在我们可以开始修剪了。

# Let's get the starting pixel coordiantes (top left of cropped top) 
start_row, start_col = int(0), int(0) 
# Let's get the ending pixel coordinates (bottom right of cropped top) 
end_row, end_col = int(height * .5), int(width) 
cropped_top = image[start_row:end_row , start_col:end_col] 
print start_row, end_row 
print start_col, end_col 

cv2.imshow("Cropped Top", cropped_top) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

# Let's get the starting pixel coordiantes (top left of cropped bottom) 
start_row, start_col = int(height * .5), int(0) 
# Let's get the ending pixel coordinates (bottom right of cropped bottom) 
end_row, end_col = int(height), int(width) 
cropped_bot = image[start_row:end_row , start_col:end_col] 
print start_row, end_row 
print start_col, end_col 

cv2.imshow("Cropped Bot", cropped_bot) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

最后,我们可以使用image.size来给出使用每个部分的像素数量。

cropped_top.size 
cropped_bot.size 

你可以用轮廓做同样的事情,但它会涉及包围盒。

1

为了简化@ avereux的回答是:

在Python中你可以使用拼接打破图像到子图像。语法如下:

sub_image = full_image[y_start: y_end, x_start:x_end] 

请注意,对于图像,原点是图像的左上角。因此,图像第一行(即最上面一行)上的像素将具有坐标x_coordinate = x,y_coordinate = 0

要获取图像的形状,请使用image.shape。这返回(no_of_rows, no_of_cols)

您可以使用这些来打破您想要的任何方式的图像。