MATLAB: How to remove error “Integers can only be combined with integers of the same class, or scalar doubles.”

errorimage processingImage Processing Toolboxsteganographytypecastingwatermarking

When I'm using typecasting my image quality is affected and I'm not getting desired results. Here is my code:
clc
[file path]=uigetfile('*.*');
img=imread(file);
[r c p]=size(img);
[pl1 pl2 pl3 pl4 pl5 pl6 pl7 pl8]=bitplane_slice(img);
figure;
subplot(3,3,1);imshow(pl1);title('pln 1')
subplot(3,3,2);imshow(pl2);title('pln 2')
subplot(3,3,3);imshow(pl3);title('pln 3')
subplot(3,3,4);imshow(pl4);title('pln 4')
subplot(3,3,5);imshow(pl5);title('pln 5')
subplot(3,3,6);imshow(pl6);title('pln 6')
subplot(3,3,7);imshow(pl7);title('pln 7')
subplot(3,3,8);imshow(pl8);title('pln 8')
sec=imread('ab.png');
new_sec=imresize(sec,[r c]);
stego = new_sec+pl2*2+pl3*4+pl4*8+pl5*16+pl6*32+pl7*64+pl8*128;
figure;
subplot(1,2,1);imshow(new_sec);title('Sec image')
subplot(1,2,2);imshow(uint8(stego));title('Stego image')
[pl1 pl2 pl3 pl4 pl5 pl6 pl7 pl8]=bitplane_slice(stego);
figure;
subplot(3,3,1);imshow(pl1);title('pln 1')
subplot(3,3,2);imshow(pl2);title('pln 2')
subplot(3,3,3);imshow(pl3);title('pln 3')
subplot(3,3,4);imshow(pl4);title('pln 4')
subplot(3,3,5);imshow(pl5);title('pln 5')
subplot(3,3,6);imshow(pl6);title('pln 6')
subplot(3,3,7);imshow(pl7);title('pln 7')
subplot(3,3,8);imshow(pl8);title('pln 8')

Best Answer

Because you're essentially adding two images with the least significant bit of one zeroed out, you're most likely going to get overflow which, for a uint8 image, means that you'll clip at 288. Cast everything to double before summing to avoid the error.