Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Matlab- Improve code in image cropping black border

Date: 2022-12-21 12:47:26

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 ? 

Expert Answer:

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.

Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

Our Comprehensive Matlab Assignment Help Services

Personalized Tutoring:Our team of MATLAB experts offers one-on-one tutoring sessions tailored to your specific needs. Whether you’re struggling with basic concepts or advanced algorithms, we provide clear, step-by-step guidance to help you understand and master MATLAB.

Assignment Assistance:Facing tight deadlines or complex assignments? We’re here to help! From initial problem analysis to code development and debugging, we offer full-spectrum support to ensure your assignments meet the highest standards.

Project Development: Need help with a research project? Our specialists can assist you in designing and implementing robust MATLAB solutions. We cover everything from project planning and data collection to coding, simulation, and result analysis.

Coursework Support: We provide comprehensive support for your coursework, helping you understand lectures, complete lab exercises, and prepare for exams. Our goal is to ensure you grasp the core principles and practical applications of MATLAB.

Thesis and Dissertation Guidance:Writing a thesis or dissertation? Our experts can assist you in incorporating MATLAB for data analysis, modeling, and simulation. We help you develop a strong methodological framework and ensure your research stands out.

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com