Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在边缘附近找到黑色区域_Python_Matlab_Opencv_Image Processing - Fatal编程技术网

Python 如何在边缘附近找到黑色区域

Python 如何在边缘附近找到黑色区域,python,matlab,opencv,image-processing,Python,Matlab,Opencv,Image Processing,我有一个像下面这样的图像。它在图像的顶部和右侧有黑色边框/区域。我希望能够找到这些区域,如第二张图片所示。注:这些区域应始终为矩形(即矩形)。我希望能够使用“不使用photoshop的代码进行图像处理”(如matlab、c#或opencv)来实现这一点 我对“图像处理”很陌生。我试图找到所有具有(0,0,0)rgb值的像素。但是,因为在噪声部分(以及图像中的任何其他位置)有太多这些黑色值。我的结果区域还包含这些不需要的区域 ----------编辑------------------ 谢谢你

我有一个像下面这样的图像。它在图像的顶部和右侧有黑色边框/区域。我希望能够找到这些区域,如第二张图片所示。注:这些区域应始终为矩形(即矩形)。我希望能够使用“不使用photoshop的代码进行图像处理”(如matlab、c#或opencv)来实现这一点

我对“图像处理”很陌生。我试图找到所有具有(0,0,0)rgb值的像素。但是,因为在噪声部分(以及图像中的任何其他位置)有太多这些黑色值。我的结果区域还包含这些不需要的区域

----------编辑------------------ 谢谢你的评论/回答。然而,我有很多这样的图片。其中一些是旋转的,这更难处理。我刚刚上传了一个,如下所示


使用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')
下面是在MATLAB中的实现。查找零列和零行,并使用它们绘制边框

用于旋转图像(也适用于非旋转图像)


请显示您的尝试。@JulienBernu刚刚编辑了我的帖子,只选择所有点都为0的行/列。严格来说,这不是正确的答案,因为如果这些行/列碰巧没有斑点,它还会删除图像中的行/列。我同意。但是我检查了
np.sum(灰色,轴=1)=0
数组和所有
False
值都位于数组的顶部。同样,对于
np.sum(灰色,轴=1)=0
Lemme修改代码以确保只省略从上/右开始的连续行/ROL。@TasosPapastylianou已修复。@SauravGupta感谢您的回答。当我看到你的第一句话时,我的心都快炸了。@tasospapstyllanou说的也可能是真的。这不是我唯一的印象。我有很多这样的图片。谢谢你的回答。SauravGupta给出了一个类似的答案,以便更早地找到非零列/行。像我上面对他的回答的评论一样,我已经旋转了图像。你有解决方案吗?这可以通过做一些像
min(logical(sum(img,2)).[1:size(img,1)])
之类的事情来很容易地矢量化。问题是我们正在做很多假设“这些黑色区域在哪里”,有多少”,等等。我确信专门用于此图像的内容不会适用于OP集合中的所有图像。@TasosPapastylianou感谢您的评论。我不明白“矢量化”是什么意思。但基本上我的图像可以像上面两组一样进行分类(旋转,而不是旋转)。旋转的图像可以有不同的角度(PS:我不知道角度)。@wildcolor为旋转图像添加了如下解决方案:well@be_good_do_good谢谢你的精彩回答。你能解释一下吗?
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;
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;