Hi, I need a method to display the modified patches of an image in MATLAB within the original image. I randomly select patches from an image, brighten half of the patches, and darken the other half. However, when I use the imshow(A) function, it shows me the original image instead of the modified one. I need a method that will display the modified patches in their original positions within the image. Here is my Coding! > clc; A=imread('C:\Users\hp\Desktop\matlab\pictures\lenna.png');%sample image rnd_x = randperm(size(A,1)-128,7);%choose 7 random unique points on x-axis rnd_y = randperm(size(A,2)-128,7);%choose 7 random unique points on y-axis image(A) for ii = 1:4 for jj = 5:7 piece{jj} = A((rnd_x(jj):(rnd_x(jj)+127)),(rnd_y(jj):(rnd_y(jj)+127)),1:3)+100; figure(jj) a=imadjust(jj); imshow(piece{jj}); end piece{ii} = A((rnd_x(ii):(rnd_x(ii)+127)),(rnd_y(ii):(rnd_y(ii)+127)),1:3)-100;%Convert chosen numbers to image pieces figure(ii) b=imadjust(ii); imshow(piece{ii}); end imshow(A)
Prashant Kumar answered .
2025-11-20
I understand that you want to take 7 patches of size 127X127 from the original image and make 3 of them lighter by adding 100 to there pixel values and 4 of them darker by subtracting 100 from there pixel values. You are not able to see any change in the image ‘A’ as you didn’t make any changes on it, rather stored the changed pixel values in ‘piece’. You can try this modified code.
clc;
A=imread('C:\Users\hp\Desktop\matlab\pictures\lenna.webp');%sample image
rnd_x = randperm(size(A,1)-128,7);%choose 7 random unique points on x-axis
rnd_y = randperm(size(A,2)-128,7);%choose 7 random unique points on y-axis
image(A)
for ii = 1:4
for jj = 5:7
piece{jj} = A((rnd_x(jj):(rnd_x(jj)+127)),(rnd_y(jj):(rnd_y(jj)+127)),1:3)+100;
A((rnd_x(jj):(rnd_x(jj)+127)),(rnd_y(jj):(rnd_y(jj)+127)),1:3)= piece{jj}; % add the changed pixel values to the original image A
figure(jj)
a=imadjust(jj);
imshow(piece{jj});
end
piece{ii} = A((rnd_x(ii):(rnd_x(ii)+127)),(rnd_y(ii):(rnd_y(ii)+127)),1:3)-100;%Convert chosen numbers to image pieces
A((rnd_x(ii):(rnd_x(ii)+127)),(rnd_y(ii):(rnd_y(ii)+127)),1:3)= piece{ii}; % add the changed pixel values to the original image A
figure(ii)
b=imadjust(ii);
imshow(piece{ii});
end
imshow(A)