How to Show modified image in MATLAB.

Illustration
John_schwan - 2021-09-07T14:57:06+00:00
Question: How to Show modified image in MATLAB.

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)  

Expert Answer

Profile picture of Prashant Kumar 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)
Still, this might not be the best way to darken or lighter an image. AS by adding 100 or subtracting 100 you might land on pixel values greater than 255 or less than 0 respectively. Thus, making those portions completely white or black.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!