How to join two codes of images?

1 view (last 30 days)
Karina
Karina on 17 Jun 2014
Commented: Image Analyst on 19 Jun 2014
I wrote this code in a function m file:
function preprocesado(NombreImagOriginal)
ImagenOriginal=imread(NombreImagOriginal);
%Convert to a gray scale
imOrGris=rgb2gray(ImagenOriginal);
%Otsu method
Ib=graythresh(imOrGris);
BN = im2bw(imOrGris,Ib);
%Mask 40 x 40
Ibmask=medfilt2(BN,[40 40]);
% ROI
iROI=roicolor(Ibmask,1);
%Cortar
imcrop(iROI);
Then I copied the coordinates of the ROI in the next small code:
I=imread('NombreImagOriginal'); %(ie. '5332.jpg')
>> I2=imcrop(I,[1359.5 549.5 1170 1224]);
>> imshow(I2)
Then I got an image like this:
In the picture there is not the white background but if you copy-paste the code you will see it. Now my problem is that I don't know if there is a way to join the two codes, and not copy-paste the coordinates. Also I want to disappear the white background because I just want the image in order to apply another code. Because if I keep the white background I get the next image when I apply a code for the red channel.
Actually I disappear the white background manually but I want to know if there is a manner to do it automatically.
Thank you for your time.

Accepted Answer

Marta Salas
Marta Salas on 18 Jun 2014
Edited: Marta Salas on 18 Jun 2014
In order to not copy manually the coordinates of your ROI, you have to keep them on a variable and return the variable as an output of your function, as follow:
function BoundingBox = preprocesado(NombreImagOriginal)
ImagenOriginal= imread(NombreImagOriginal);
%Convert to a gray scale
imOrGris=rgb2gray(ImagenOriginal);
%Otsu method
Ib=graythresh(imOrGris);
BN = im2bw(imOrGris,Ib);
%Mask 40 x 40
Ibmask=medfilt2(BN,[40 40]);
% ROI
iROI=roicolor(Ibmask,1);
%Cortar y guarda las cordenadas de la región de interes
[~, BoundingBox] = imcrop(iROI);
Then you can call your function:
filename = 'peppers.png';
BoundingBox = preprocesado(filename);
I=imread(filename);
I2=imcrop(I,BoundingBox);
imshow(I2)
Although for completeness you could crop the image inside your function "preprocesado"
function [ImageCropped,BoundingBox] = preprocesado(NombreImagOriginal)
ImagenOriginal= imread(NombreImagOriginal);
%Convert to a gray scale
imOrGris=rgb2gray(ImagenOriginal);
%Otsu method
Ib=graythresh(imOrGris);
BN = im2bw(imOrGris,Ib);
%Mask 40 x 40
Ibmask=medfilt2(BN,[40 40]);
% ROI
iROI=roicolor(Ibmask,1);
% Guarda las cordenadas de la región de interes
[~, BoundingBox] = imcrop(iROI);
% Corta la imagen original
ImageCropped=imcrop(ImagenOriginal,BoundingBox);
Then, your example becomes:
filename = 'peppers.png';
[I2,BoundingBox] = preprocesado(filename);
imshow(I2)
Regarding the white background, I am afraid you saved the image with "save as" and this is the reason your image got the white frame. If this the case, could you apply the second code directly to I2 which actually has no white frame?. If you actually didn't save the image, you will need to elaborate what you mean by "white background"
  3 Comments
Marta Salas
Marta Salas on 19 Jun 2014
Edited: Marta Salas on 19 Jun 2014
Instead of "save as" a png, you can save the variable I2 to a mat-file that you can load whenever you need it:
save('mymatfile.mat','I2')
load('mymatfile.mat')
Image Analyst
Image Analyst on 19 Jun 2014
You didn't mention anything about saving in your original post. Anyway, simply use imwrite() if you need to save the image into a standard image format. If you have floating point images you can use save() to save into a proprietary .mat format file.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 17 Jun 2014
I'd like to help but I don't know how. What white background you're talking about? All I see is an orange image (presumably the original image), and a pseudocolored image. I don't see any cropped image or image with a white background. Please attach your script and original image.
Do you want to use fixed cropping coordinates? Or do you want to have the user select where to crop?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!