SEB*_*TuS 2 matlab image image-processing matlab-guide
我有这个代码在我的图片中裁剪黑色边框.
我不知道为什么边界仍然存在.
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');
Run Code Online (Sandbox Code Playgroud)
我该如何更改此代码?
假设您的黑色边框像素都为零,此代码适用于我.如果图像的黑色边框中存在非零像素(可能是由于量化和压缩伪像 - 毕竟你的图像是JPEG ......),那么这段代码将无效.这段代码正在做的是首先检查所有列,看是否有任何非零像素.然后通过确定第一个非零列并转到最后一个非零列来确定要裁剪的位置.此代码还假定非零列是对称的,这就是您在zeros
语句中除以2的原因.顺便说一句,zeros
是MATLAB中的内置函数.我不建议您创建具有此名称的变量,因为您的后续代码可能需要此功能,并且您无意中使用变量遮盖此函数.
不过,这就是我测试你的代码是否有效的方法.我使用了MATLAB系统路径中的内置图像cameraman.tif
,并在图像周围创建了一个10像素的边框.然后我运行你的代码,看看我会得到什么:
im = imread('cameraman.tif');
I1 = padarray(im2double(im), [10 10]);
Run Code Online (Sandbox Code Playgroud)
运行你的代码,这是我得到的数字:
运行该代码时存在某些先决条件,因此在使用之前请记住这一点:
因此,我建议您将图像的阈值设置为少量(可能是强度为10),以确保在继续之前边界为零.然后,您将使用此图像计算您拥有的边框像素数.因此,做这样的事情.
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');
Run Code Online (Sandbox Code Playgroud)
根据您的评论,您说过您的黑色边框可能不对称.在这种情况下,您需要有逻辑来确定黑色边框到黑色边框末端的位置的起点.您将此应用于黑色边框的行和列.在这种情况下,我将使用该find
命令,并使用any
沿行和列的操作,并确定行和列非零的最小和最大索引.这完全对应于MATLAB中与代码相关的最小和最大操作.我将使用带有阈值处理的修改算法来规避任何量化或压缩伪像.因此:
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');
Run Code Online (Sandbox Code Playgroud)
上面的代码现在适用于任何大小的黑色边框.
归档时间: |
|
查看次数: |
1998 次 |
最近记录: |