Histogram-based segmentation error

Hey guys, So I'm working on histogram-based segmentation program using MatLab and I am able to successfully assign ranges to the histogram and obtain the corresponding binary images (as seen below),
but when I implemented the following code:
MATLAB code:
m1 = sum(sum(B1.*A))/sum(sum(B1));
m2 = sum(sum(B2.*A))/sum(sum(B2));
m3 = sum(sum(B3.*A))/sum(sum(B3));
m4 = sum(sum(B4.*A))/sum(sum(B4));
m5 = sum(sum(B5.*A))/sum(sum(B5));
m6 = sum(sum(B6.*A))/sum(sum(B6));
C = (m1 * B1) + (m2 * B2) + (m3 * B3) + (m4 * B4) + (m5 * B5) + (m6 * B6);
figure, subplot(2,1,1), imshow(A), title('Original Image');
subplot(2,1,2), imshow(C), title('Histogram-based segmentation');
And I got the following error, "Error using . Integers can only be combined with integers of the same class, or scalar doubles." % Error in A3_Prob2 (line 46) m1 = sum(sum(B1.*A))/sum(sum(B1));*
so I tweaked my code, which is as follows,
MATLAB code:
m1 = sum(sum(B1.*(double(A))))/sum(sum(B1));
m2 = sum(sum(B2.*(double(A))))/sum(sum(B2));
m3 = sum(sum(B3.*(double(A))))/sum(sum(B3));
m4 = sum(sum(B4.*(double(A))))/sum(sum(B4));
m5 = sum(sum(B5.*(double(A))))/sum(sum(B5));
m6 = sum(sum(B6.*(double(A))))/sum(sum(B6));
C = (m1 * B1) + (m2 * B2) + (m3 * B3) + (m4 * B4) + (m5 * B5) + (m6 * B6);
subplot(2,1,1), imshow(A), title('Original Image');
subplot(2,1,2), imshow(C), title('Histogram-based segmentation');
And I got the output image as seen below.I know the histogram-based segmented image won't be just "white", So where am I going wrong?? Could someone highlight the error in my coding/guide me please?
Thanks

2 Commenti

What is A and B1, .... B6?
A is the image read and B1 - B6 are the ranges assigned. Here is my program for reference,
MATLAB code:
clear all; close all;
A = imread('Cman.tif');
imhist(A),figure, imshow(A);
fprintf('Please click enter to view the ranges of the image \n');
pause;
close all;
R1=[0,30];
R2=[31,50];
R3=[51,80];
R4=[81,145];
R5=[146,197];
R6=[198,255];
B1=255*((A>=0)&(A<=30));
figure,subplot(2,3,1),imshow(B1), title('R1=[0,30]');
fprintf('Please click enter to view B2 region of the image \n');
pause;
B2=255*((A>=31)&(A<=50));
subplot(2,3,2),imshow(B2), title('R2=[31,50]');
fprintf('Please click enter to view B3 region of the image \n');
pause;
B3=255*((A>=51)&(A<=80));
subplot(2,3,3),imshow(B3),title('R3=[51,80]');
fprintf('Please click enter to view B4 region of the image \n');
pause;
B4=255*((A>=81)&(A<=145));
subplot(2,3,4),imshow(B4),title('R4=[81,145]');
fprintf('Please click enter to view B5 region of the image \n');
pause;
B5=255*((A>=146)&(A<=197));
subplot(2,3,5),imshow(B5), title('R5=[146,197]');
fprintf('Please click enter to view B6 region of the image \n');
pause;
B6=255*((A>=198)&(A<=255));
subplot(2,3,6),imshow(B6), title('R6=[198,255]');
m1 = sum(sum(B1.*A))/sum(sum(B1));
m2 = sum(sum(B2.*A))/sum(sum(B2));
m3 = sum(sum(B3.*A))/sum(sum(B3));
m4 = sum(sum(B4.*A))/sum(sum(B4));
m5 = sum(sum(B5.*A))/sum(sum(B5));
m6 = sum(sum(B6.*A))/sum(sum(B6));
C = (m1 * B1) + (m2 * B2) + (m3 * B3) + (m4 * B4) + (m5 * B5) + (m6 * B6);
figure, subplot(1,1,1), imshow(A), title('Original Image');
subplot(1,1,2), imshow(C), title('Histogram-based segmentation');

Accedi per commentare.

 Risposta accettata

Thorsten
Thorsten il 16 Ott 2015
Modificato: Thorsten il 16 Ott 2015
Use
imshow(C, [])
Because you scaled B with 255, the m values are within the range [0 255], so your C is in the range [0,256^2].
You can improve your code by computing the B without the 255 scaling
B1= A >= 0 & A <= 30;
and adding
A = im2double(A);
before computing the m-values. And the m-values can be computed as
m1 = sum2(B1.*A)/nnz(B1);
Then C is in the range [0,1], and can be viewed with
imshow(C)
And of course if would be better if you vectorize your code.

Più risposte (0)

Categorie

Scopri di più su Images in Centro assistenza e File Exchange

Richiesto:

il 16 Ott 2015

Commentato:

il 16 Ott 2015

Community Treasure Hunt

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

Start Hunting!

Translated by