How to join two codes of images?

조회 수: 1 (최근 30일)
Karina
Karina 2014년 6월 17일
댓글: Image Analyst 2014년 6월 19일
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.

채택된 답변

Marta Salas
Marta Salas 2014년 6월 18일
편집: Marta Salas 2014년 6월 18일
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
Marta Salas
Marta Salas 2014년 6월 19일
편집: Marta Salas 2014년 6월 19일
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 2014년 6월 19일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 6월 17일
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?

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by