Struggling to subtract 2 images

2 views (last 30 days)
Nicholas Lund
Nicholas Lund on 8 Jun 2020
Answered: Image Analyst on 12 Jun 2020
This is the code i am trying to run, it runs the blurpic fine but can't get the subtraction to work without "Array dimensions must match for binary array op" and "Error in task3 (line 12), y = (pic) - (blurpic);" not sure what to do now.
clear all
pic = double(imread("IMG_5698.JPG")); % original picture used
s = size(pic);
j = 2:s(1) - 1;
k = 2:s(2) - 1;
blurpic(j,k,:) = (pic(j,k,:) + pic(j - 1,k,:) + pic(j + 1,k,:) + ...
pic(j,k - 1,:) + pic(j,k - 1,:) + pic(j,k + 1,:))/5;
%image(uint8(blurpic))
y = (pic) - (blurpic);
%image(uint8(y))
intensepic = y(1:0.1:end, 1:0.1:end, :);
%image(uint8(intensepic))
output = intensepic + original
image(uint8(output))

Answers (2)

Umar farooq Mohammad
Umar farooq Mohammad on 10 Jun 2020
As you are trying to create a blurpic using your own defult code where you are calcualting the mean of all nearby surrounding pixels. But according to your code the dimentsions are being reduced.
So i suggest you
1) to change the code inorder to handle the pixels at edges aswell so that you dont have to supress the dimensions to 2:s(1)-1.
3) or else you can create an empty image of the dimensions of original and then add blurred pixels to it so as to work with your code. as follows... ( but i suggest you to use any onne from above two options)
clear all
pic = double(imread("C:\Users\umarf\Downloads\Umar Farooq\Photoes\1.jpg")); % original picture used
s = size(pic);
j = 2:s(1)-1 ;
k = 2:s(2)-1 ;
blurpic = zeros(s);
blurpic(j,k,:) = (pic(j,k,:) + pic(j - 1,k,:) + pic(j + 1,k,:) + pic(j,k - 1,:) + pic(j,k - 1,:) + pic(j,k + 1,:))/5;
%image(uint8(blurpic))
y = (pic) - (blurpic);
-Umar

Image Analyst
Image Analyst on 12 Jun 2020
Try this:
rgbImage = imread('peppers.png');
subplot(2, 1, 1);
imshow(rgbImage);
% Now blur it
kernel = [1, 0, 1; 0, 1, 0; 1, 0, 1];
kernel = kernel / sum(kernel(:)); % Normalize so mean intensity does not change.
blurredPic = imfilter(rgbImage, kernel);
subplot(2, 1, 2);
imshow(blurredPic);
impixelinfo

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!