Matlab-改善图像裁剪黑色边框中的代码 [英] Matlab- Improve code in image cropping black border

查看:248
本文介绍了Matlab-改善图像裁剪黑色边框中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码在我的图片中裁剪黑色边框。



我不知道为什么边框仍然存在。

  I1 = im2double(imread('dart.jpg')); 

sizeI = size(I1);
zeros = floor((sizeI(2) - min(sum(any(I1))))/ 2);
I2 = I1(:,零:sizeI(2)-zeros,:);
nonZero = sum(any(I1,2));


sizeI2 = size(I2);
zerosRows = floor((sizeI(1) - min(sum(any(I2,2))))/ 2);
I3 = I2(zerosRows:sizeI2(1)-zerosRows,:,:);

subplot(1,3,1),imshow(I1); title('Figure 1');
subplot(1,3,2),imshow(I2); title('Figure 2');
subplot(1,3,3),imshow(I3); title('图3');

如何更改此代码?

解决方案

此代码适用于我,假设您的黑色边框像素全为零。如果图像的黑色边框中存在非零像素(可能是由于量化和压缩效果 - 您的图像毕竟是 JPEG ...),则此代码将无效。这段代码正在做的是首先检查所有列,看看是否有任何非零像素。然后通过确定第一个非零列并转到最后一个非零列来确定要裁剪的位置。此代码还假定非零列是对称,这就是您在 zeros 语句中除以2的原因。顺便说一下,命令,并沿行和列使用任何操作,并确定行和列非零的最小和最大索引。这完全对应于MATLAB中与代码相关的最小和最大操作。我将使用带有阈值处理的修改算法来规避任何量化或压缩伪像。因此:

  I1 = im2double(imread('dart.jpg')); %//读入图片
I1thresh = I1> =(10/255); %//作为示例 - 请注意除以255,因为你做了im2double
%//已删除,因为不再需要
%// sizeI = size(I1);
nonZeroCols = find(any(I1thresh)); %//更改
minCol = min(nonZeroCols); %//更改
maxCol = max(nonZeroCols); %//更改
I2 = I1(:,minCol:maxCol,:);
I2thresh = I1thresh(:,minCol:maxCol,:); %//注意新变量
%//注释掉。不要在任何地方看到它被使用
%// nonZero = sum(any(I1thresh,2)); %//注意任何

%的变化//已删除,因为不再需要
%// sizeI2 = size(I2);
nonZeroRows = find(any(I2thresh,2)); %//更改
minRow = min(nonZeroRows); %//更改
maxRow = max(nonZeroRows); %//更改
I3 = I2(minRow:maxRow,:,:); %//更改

subplot(1,3,1),imshow(I1); title('图1');
subplot(1,3,2),imshow(I2); title('Figure 2');
subplot(1,3,3),imshow(I3); title('图3');

以上代码现在适用于任何尺寸的黑色边框。


I have this code that crop the black borders in my picture.

I don't know why the border still exists.

I1=im2double(imread('dart.jpg'));

sizeI = size(I1);
zeros = floor((sizeI(2) -  min(sum(any(I1))))/2);
I2 = I1(:, zeros : sizeI(2)-zeros, :);
nonZero = sum(any(I1,2));


sizeI2 = size(I2);
zerosRows = floor((sizeI(1) -  min(sum(any(I2, 2))))/2);
I3 = I2(zerosRows : sizeI2(1)-zerosRows, :, :);

subplot(1,3,1), imshow(I1);title('Figure 1');
subplot(1,3,2), imshow(I2);title('Figure 2');
subplot(1,3,3), imshow(I3);title('Figure 3');

How can I change this code ?

解决方案

This code works for me, assuming that your black border pixels are all zero. Should there be non-zero pixels in the black borders of your image (probably due to quantization and compression artifacts - your image is JPEG after all...), then this code will not work. What this code is doing is checking all of the columns first to see if there are any non-zero pixels. It then figures out where to crop by determining the first non-zero column and going to the last non-zero column. This code also assumes that the non-zero columns are symmetric, which is why you're dividing by 2 in your zeros statement. By the way, zeros is a built-in function in MATLAB. I don't recommend you create a variable that has this name, as your later code may require this function and you are unintentionally shadowing over this function with a variable.

Nevertheless, here's what I did to test to see if your code works. I used a built-in image from MATLAB's system path cameraman.tif, and created a 10 pixel border surrounding the image. I then ran your code to see what I would get:

im = imread('cameraman.tif');
I1 = padarray(im2double(im), [10 10]);

Running your code, this is the figure I get:


There exist certain preconditions when running that code so bear this in mind before using it:

  1. This assumes an entirely symmetric black border around the image.
  2. This assumes that all black border pixels are zero.
  3. If your border has non-zero pixels, even though it may visually look like there are non-zero pixels, then this code will not work.

As such, I suggest you threshold your image by a small amount (perhaps intensity 10) to ensure that the borders are zero before you proceed. You would then use this image to calculate how many border pixels you have. As such, do something like this.

I1=im2double(imread('dart.jpg')); %// Read in the image
I1thresh = I1 >= (10/255); %// As an example - Note the division by 255 as you did im2double
sizeI = size(I1);
zeros = floor((sizeI(2) -  min(sum(any(I1thresh))))/2); %// Note the change in any
I2 = I1(:, zeros : sizeI(2)-zeros, :); 
I2thresh = I1thresh(:, zeros : sizeI(2)-zeros, :);  % // Note new variable
nonZero = sum(any(I1thresh,2)); %// Note the change in any

sizeI2 = size(I2);
zerosRows = floor((sizeI(1) -  min(sum(any(I2thresh, 2))))/2); %// Note change in any
I3 = I2(zerosRows : sizeI2(1)-zerosRows, :, :);

subplot(1,3,1), imshow(I1);title('Figure 1');
subplot(1,3,2), imshow(I2);title('Figure 2');
subplot(1,3,3), imshow(I3);title('Figure 3');


Edit - Non-uniform black borders

From your comments, you have said that your black borders may not be symmetric. In that case, you need to have logic that determines the beginning of where the black borders are to where the end of the black borders are. You apply this for both the rows and the columns of the black borders. In that case, I would use the find command, and use the any operation along the rows and columns and determine the smallest and largest indices where the rows and columns are non-zero. This exactly corresponds to the minimum and maximum operations in MATLAB with respect to your code. I'm going to use the modified algorithm with thresholding to circumvent any quantization or compression artifacts. As such:

I1=im2double(imread('dart.jpg')); %// Read in the image
I1thresh = I1 >= (10/255); %// As an example - Note the division by 255 as you did im2double
%// Removed as this is no longer needed 
%// sizeI = size(I1);
nonZeroCols = find(any(I1thresh)); %// Change
minCol = min(nonZeroCols); %// Change
maxCol = max(nonZeroCols); %// Change
I2 = I1(:, minCol : maxCol, :); 
I2thresh = I1thresh(:, minCol : maxCol, :);  % // Note new variable
%// Commented out. Don't see this being used anywhere
%//nonZero = sum(any(I1thresh,2)); %// Note the change in any

%// Removed as this is no longer needed
%//sizeI2 = size(I2);
nonZeroRows = find(any(I2thresh, 2)); %// Change
minRow = min(nonZeroRows); %// Change
maxRow = max(nonZeroRows); %// Change
I3 = I2(minRow : maxRow, :, :); %// Change

subplot(1,3,1), imshow(I1);title('Figure 1');
subplot(1,3,2), imshow(I2);title('Figure 2');
subplot(1,3,3), imshow(I3);title('Figure 3');

The above code should now work for any size black border.

这篇关于Matlab-改善图像裁剪黑色边框中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆