2016-07-26 59 views
-5

我有像下面這樣的圖像。它在圖像的頂部和右側有黑色邊框/區域。我希望能夠像第二張圖片中所示那樣找到這些區域。請注意,這些區域應始終是直立的(即矩形)。我希望能夠使用'不使用photoshop的代碼進行圖像處理'(例如matlab,c#或opencv)。如何在邊緣附近找到黑色區域

 Input image

 output image

我是很新的 '圖像處理'。我試圖找到所有有(0,0,0)rgb值的pionts。但是因爲在噪聲部分(以及圖像中的任何其他位置)中存在如此多的這些黑色值。我的結果區域也包含這些不需要的區域....

----------編輯--------------- 感謝您的所有意見/答覆。但是,我有很多這些圖像。其中一些是旋轉的,這有點難以處理。我剛剛上傳了一個,如下所示。

enter image description here

+5

請出示你的企圖。 – Julien

+0

@JulienBernu剛剛編輯我的帖子 – wildcolor

+1

只選擇所有點爲0的行/列。 – Julien

回答

1
color_img = imread('0k4Kh.jpg'); 
img = rgb2gray(color_img); 
[x, y] = size(img); 

for i = 1:x 
    if length(find(img(i, :))) ~= 0 
     lastmarginalrow = i-1; 
     break; 
    end 
end 

for ii = y:-1:1 
    if length(find(img(:, ii))) ~= 0 
     lastmarginalcol = ii-1; 
     break; 
    end 
end 

figure; 
fig = imshow(color_img); 
h = impoly(gca, [0,x; lastmarginalcol,x; lastmarginalcol,lastmarginalrow; 0,lastmarginalrow]); 
api = iptgetapi(h); 
api.setColor('red'); 
saveas(fig, 'test.jpg'); 
close all; 

這是在MATLAB中的實現。查找零列和零行,並使用它們繪製邊框。

對於旋轉的圖像(應該還沒有旋轉的工作)

color_img = imread('N6vK9.png'); 
img = rgb2gray(color_img); 
[x, y] = size(img); 

verts = []; 
% traversing through all columns 
for i = 1:y 
    % find all non-zero pixels in each column 
    nonzeros = find(img(:,i)); 
    % if all pixels are black in a column, below if condition will skip 
    if length(nonzeros) ~= 0 
     % if there is atleast one non-zero pixel, not that co-oridinate/positions in matrix by appending 
     verts = [i, nonzeros(1); verts]; 
    end 
end 

figure; 
fig = imshow(color_img); 
% polygon based on first and last vertix/co-ordinate of found non-zero co-ordinates 
% Assumed that it was slanted straight line, used last and first co-ordinate. If it is curvy border, anyways we have all veritices/co-ordinates of first non-zero pixel in all columns. 
h = impoly(gca, [verts(1,:); verts(length(verts), :); 1,x; verts(1),x]); 
api = iptgetapi(h); 
api.setColor('red'); 
saveas(fig, 'test.jpg'); 
close all; 

enter image description here

+0

謝謝你的答案。SauravGupta也給出了類似的答案,以找到非零列/行ealier以及我的上述評論他的回答,我已經旋轉的圖像。解決方案來處理它們? – wildcolor

+0

這可以通過像'min(logical(sum(img,2))。* [1:size(img,1)]')等方式進行矢量化。問題是我們'重新做出很多假設「在哪裏」這些黑色區域是「多少」等等。我確定專門用於該圖像的內容不適用於OP的所有圖像。 –

+0

@TasosPapastylianou感謝您的評論。我不明白'矢量化'是什麼意思,但基本上我的圖像可以像上面兩組一樣進行分類(旋轉,不旋轉),旋轉後的可以有不同的角度(PS:我不知道角度) – wildcolor

1

使用Python2.7 + OpenCV3。這個想法是隻保留非零的行和列。代碼如下。

import cv2 
import numpy as np 
#Read in the image 
arr = np.array(cv2.imread('image.jpg')) 
#Convert to grayscale 
gray = np.sum(arr, axis=2) 
print gray.shape #(496, 1536) 
filter_row = np.sum(gray,axis=1)!=0 
# Assuming first few values are all False, find index of first True, and set all values True after that 
filter_row[list(filter_row).index(True):,] = True 
# Keep only non-zero rows 
horiz = gray[filter_row,:] 

filter_column = np.sum(gray,axis=0)!=0 
# Assuming first few values are all False, find index of first False, and set all values True before that 
filter_column[:list(filter_column).index(False),] = True 
# Keep only non-zero columns 
vert = horiz[:,filter_column] 
print vert.shape #(472, 1528) 
bordered = cv2.rectangle(cv2.imread('image.jpg'), (0, gray.shape[0]-vert.shape[0]), (vert.shape[1],gray.shape[0]), (255,0,0), 2) 
cv2.imwrite(bordered,'result.jpg') 
+0

嚴格來說,這不是正確的答案,因爲它也會刪除圖像內的行/列,如果它們碰巧沒有斑點。 –

+0

我同意。但是我檢查了'np.sum(gray,axis = 1)!= 0'數組,所有'False'值都位於數組的頂部。對於'np.sum(gray,axis = 1)!= 0',類似地,修改代碼以確保從頂部/右側只有連續的行/ rols被省略。 –

+0

@TasosPapastylianou固定。 –