Search code examples
scilab

Crop image in scilab


I want to crop image using mouse selection at particular region of interest in scilab,here my code is

I=imread('G:\SCI\FRAME\mixer2.jpg');  
   I1G = rgb2gray(I);
   figure();ShowImage(I1G,'mixer');
   IN1G = gca();
   rect1 = rubberbox();
   ROI1=imcrop(I1G,rect1);disp(ROI1);

But it gives the following error: The rectangle is out of the image range. and i also use xclick and xgetmouse function for cropping using mouse selection and it also gives the same error. please give me suggestions for correcting code .

Thanks and Regards


Solution

  • The problem arises from the difference between the image coordinate system (used by imcrop and all the other functions of the SIVP toolbox) and the "regular" coordinate system (used by rubberbox, xcick and all the builtin functions). Images have the first pixel at top-left. On the contrary rubberbox have the zero at bottom left. To correct this you have to reverse the y (vertical) axes coordinate before applying imcrop():

    imagefile="d:\Attila\PROJECTS\Scilab\Stackoverflow\mixer_crop.jpg"; 
    I=imread(imagefile); 
    I1G=rgb2gray(I);
    scf(0); clf(0);
    ShowImage(I1G,'mixer');
    rect1=rubberbox();
    imheight=size(I1G,"r");   //image height
    rect1(2)=imheight-rect1(2);   //reverse y axes coordinates (0 is at top)
    ROI1=imcrop(I1G,rect1);
    scf(1); clf(1);
    ShowImage(ROI1,'ROI1');