I want column matrix in for loop

1 view (last 30 days)
HEMRAJ PATEL
HEMRAJ PATEL on 30 Nov 2021
Commented: HEMRAJ PATEL on 1 Dec 2021
I want to create a matrix where the R (subtracted) matrix is stored in C matrix such that all 9 R(i,j) elements produced from 10 images get stored in first column of matrix c, then next R(i,j) be stored in 2nd column of matrix c and so on.
Here x is row no.
and y is column no.
use whatever image you like with 400X400 pixels
n=10
for x=11:1:13
for y=11:1:13
for i=1:1:9
for k =1:1:(n-1)
image1 = double(imread(sprintf('image_%.4d.jpg',k)));
I1=image1(1:400,1:400);
image2 = double(imread(sprintf('image_%.4d.tiff',k+1)));
I2=image2(1:400,1:400);
R= imsubtract(I2,I1);
c(k,i)=R(x,y);
end
end
end
end

Answers (1)

DGM
DGM on 30 Nov 2021
Edited: DGM on 30 Nov 2021
I'm not sure, but this might be close to what you want.
n=10
xrange = 11:13;
yrange = 11:13;
for k = 1:(n-1)
image1 = imread(sprintf('image_%.4d.jpg',k));
image2 = imread(sprintf('image_%.4d.tiff',k+1));
I1 = double(image1(xrange,yrange));
I2 = double(image2(xrange,yrange));
c(k,:) = I2(:)-I1(:);
end
  5 Comments
DGM
DGM on 1 Dec 2021
That's obviously not going to work if I2,I1 don't exist.
If the goal is to avoid the issues caused by data scaling, I usually recommend to simply use im2double(). I guess I didn't bother.
n=10
xrange = 11:13;
yrange = 11:13;
for k = 1:(n-1)
image1 = imread(sprintf('image_%.4d.jpg',k));
image2 = imread(sprintf('image_%.4d.tiff',k+1));
I1 = im2double(image1(xrange,yrange));
I2 = im2double(image2(xrange,yrange));
c(k,:) = I2(:)-I1(:);
end
or you could use imsubtract()
n=10
xrange = 11:13;
yrange = 11:13;
for k = 1:(n-1)
image1 = imread(sprintf('image_%.4d.jpg',k));
image2 = imread(sprintf('image_%.4d.tiff',k+1));
D = imsubtract(image2(xrange,yrange),image1(xrange,yrange));
c(k,:) = D(:);
end
If the image classes differ, that will at least cast and scale them so that they're compatible, but the output scale and class will vary with whatever the inputs are. You could cast D with im2double() or the like, but it seems unnecessary when the output class can be asserted to begin with.
That said, OP hasn't mentioned what the problem actually is.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!