Trying to display the segmentation result in the same type as of the original image.
    1 view (last 30 days)
  
       Show older comments
    
I have applied basic segmentation on my image. 's1.png' is the original image and 's2.png' is the segmented image. I want the segmented image to be of the same type(color) as the original image. However, I am unable to do it. Any suggestions would be appreciated.
m=imread('s1.png');
se = strel('disk',10);
closeBW = imclose(m,se);
figure, imshow(closeBW)
level = multithresh(closeBW);
seg_I = imquantize(closeBW,level);
figure, imshow(seg_I,[])
img_class1=class(m);
fill1=cast(seg_I,img_class1);
m1=fill1.*m;
figure,imshow(fill1,[])
6 Comments
  DGM
      
      
 on 3 Nov 2022
				Since I don't really understand the use of multithresh() and imquantize() in this method, I don't know if that's appropriate or not.  You certainly could do morphological operations on any image; I just don't know if it's appropriate in this context.
Answers (1)
  DGM
      
      
 on 2 Nov 2022
        
      Edited: DGM
      
      
 on 2 Nov 2022
  
      I think you might be asking how to programmatically cast and scale an image to a specific class from any class.  If that's the case, then this is how you'd do it within the scope of base MATLAB/IPT:
% say we want to cast/scale image FG to have the same class as image BG
outclass = class(BG);
switch outclass
    case 'uint8'
        FG = im2uint8(FG);
    case 'double'
        FG = im2double(FG);
    case 'single'
        FG = im2single(FG);
    case 'uint16'
        FG = im2uint16(FG);
    case 'int16'
        FG = im2int16(FG);	
    case 'logical'
        FG = logical(FG); % this is a great way to create problems
    otherwise
        error('some error message goes here')
end
... because the primary parameter (the class name) is embedded in the function names.  It's almost like you're being tempted to use eval() or something. 
You could also use cast(), which does support the class name as a parameter, but then you'll have to take care to correctly scale the data for each possible combination, because cast() doesn't scale anything.  
FG = imcast(FG,class(BG));
That said, you might presume that your input image is uint8 and just use im2uint8().  That's certainly fallible,but it's simple and might cover your needs.
9 Comments
  Image Analyst
      
      
 on 3 Nov 2022
				Sorry we can't help you.  We don't know what the original image looks like so we don't know what you want to find in it.  Somehow you found a squiggly shape but apparently it's not good for some unknown (to us) reason.  And we don't even know what would be the perfect, ideal segmentation since you're not showing us that, so we don't know how to alter your existing, bad segmentation to give you the ideal, perfect, optimal segmentation.  Good luck though.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


