double precision becomes complex double after calculation

The outputs for a becomes complex double after calculation. However, it should be double precision since I am just raising the power of the element. Is there any possibility to fix it? .
i got a as a comlex double and in b i got NaN values.
tic
clear;
close all;
I=imread('Lenna.tiff');
if size(I,3)==3
I=rgb2gray(I);
end
k=I;
%size of image
[M,N]=size(I);
%convert to double
I=double(I);
Y=zeros(M,N);
binimg=de2bi(I,'left-msb');
binimg1 = binimg(:, 1:end-3);
binimg2 = bi2de(binimg1,'left-msb');
finalimg = reshape(binimg2,size(I));
I=finalimg;
% ----------------------------------------------------
%% Block Truncation Coding Compression
blksize=2; %Block Size
mu=colfilt(I,[blksize,blksize],'distinct',@(x) ones(blksize^2,1)*mean(x));
sigma=colfilt(I,[blksize,blksize],'distinct',@(x) ones(blksize^2,1)*std(x));
q=I>mu;
q=colfilt(q,[blksize,blksize],'distinct',@(x) ones(blksize^2,1)*sum(x));
m=blksize^2; %length*width of block
a=mu-sigma.*(sqrt(q./m-q)); %low mean
b=mu+sigma.*(sqrt(m-q./q)); %high mean
H=I>=mu; %elements of Bitmap
Y(H)=a(H);
Y(~H)=b(~H);
Y=uint8(Y);
% ----------------------------------------------------------------------------
% Dividing into blocks
% ----------------------------------------------------------------------------
if ismatrix(Y)
blocks2x2 = mat2cell(Y, 2 * ones(1,256), 2 * ones(1,256) );
else
blocks2x2 = mat2cell(Y, 2 * ones(1,256), 2*ones(1,256), size(Y,3) );
end
% -----------------------------------------------------------------------------
subplot(1,2,1),imshow(k);title('orginal image');
subplot(1,2,2),imshow(Y);title('compressed image');
%output BTC image
% dPSNR=psnrr(I,Y);
toc

3 个评论

your q looks like it will be nonnegative (possibly 0). Your low calculations have sqrt(q./m-q) . The inner expression factors to q.*(1/m-1) . With m being positive the 1/m-1 is negative . But q is nonnegative . Nonnegative times negative is nonpositive. Therefore you are taking sqrt of something that is either 0 or negative .
In your calculations of b notice that q./q is going to be 1 except when q is 0 so it is not obvious that q./q is appropriate there .
i got it sir thank you so much.
can you provide the modified code.
No, I cannot provide the modified code as I do not know the proper equations.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by