2017-06-26 124 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)

您可以使用這些來打破您想要的任何方式的圖像。