Read the the pixel value and rebuild an image

11 次查看(过去 30 天)
Hello there! I want to read the pixel value of an image and rebuild it later, but the performance of the new image is much different from the original one, although the shape is similar.
Could you help me solve this issue? Thank you very much.
Attached is my code.
clear;
clc;
[File, Path]=uigetfile('*.tif','Please select your files:', ...
'MultiSelect', 'on');
fid = imread(File);
[m,n,k]=size(fid);
Num_scan=1;
for x=1:m
for y=1:n
RGB=impixel(fid,x,y);
Pix(Num_scan,1)=x;
Pix(Num_scan,2)=y;
A_Pix0(Num_scan,1)=RGB(1);
A_Pix0(Num_scan,2)=RGB(2);
A_Pix0(Num_scan,3)=RGB(3);
Num_scan=Num_scan+1;
end
end
A_Pix=reshape(A_Pix0,[n,m,3]);
image(A_Pix)
Below are the original image and the new image.

采纳的回答

Walter Roberson
Walter Roberson 2018-7-14
You should have used
A_Pix0 = zeros(n*m, 3, class(fid));

更多回答(1 个)

Image Analyst
Image Analyst 2018-7-14
编辑:Image Analyst 2018-7-14
I don't know why you're opening a tiff format file and trying to read it and rebuilt it a pixel at a time. Simply open it as a tif image and make a copy:
[baseFileName, folder] = uigetfile('*.tif','Please select your files:', ... 'MultiSelect', 'off');
fullFileName = fullfile(folder, baseFileName);
image1 = imread(fullFileName);
A_pix = image1;
Also, fid is commonly used as a FileID, or the file handle returned by fopen(). You should choose a name like rgbImage instead of fid, because it's confusing, at least it confused me until I finally figured out what you were doing.
By the way you're making the common beginner mistake of confusing rows and columns with x and y. Arrays are indexed (row, column), NOT (x, y).
This kind of mistake arises from another common beginner mistake of choosing non-descriptive single letter variable names like m,n,x,y,i,j, etc. Notice how m is really rows but you're having x (a horizontal designation) go from 1 to rows instead of 1 to columns? Fix is below:
[rows, columns, numberOfColorChannels] = size(rgbImage)
for col = 1 : columns
for row = 1 : rows
  3 个评论
Imran Riaz
Imran Riaz 2022-8-3
@Hao Shi Can u share the complete code to rebuild new image from he pixel values of old image, I also want to do same thing. I have to discard the some portion which is informationless.
DGM
DGM 2022-8-3
编辑:DGM 2022-8-3
If you want to do the same thing as what the original post does, then you're in luck, because it basically does nothing.
% this is the original code.
% this code is so slow that it will take over 15 minutes
% to process a 384x512 image
fid = imread('peppers.png');
fid = fid(1:10,1:10,:); % no sense wasting all that time to accomplish nothing
[m,n,k]=size(fid);
Num_scan=1;
for x=1:m
for y=1:n
RGB=impixel(fid,x,y);
Pix(Num_scan,1)=x;
Pix(Num_scan,2)=y;
% reshape the image into a 3-column matrix
A_Pix0(Num_scan,1)=RGB(1);
A_Pix0(Num_scan,2)=RGB(2);
A_Pix0(Num_scan,3)=RGB(3);
Num_scan=Num_scan+1;
end
end
A_Pix=reshape(A_Pix0,[n,m,3]); % the image is just reshaped back again
% this is essentially the same thing
inpict = imread('peppers.png');
inpict = inpict(1:10,1:10,:); % use the same image region
[m,n,~] = size(inpict);
[xx yy] = meshgrid(1:n,1:m);
allpixindices = [xx(:) yy(:)]; % a pile of indices for some reason
outpict = inpict; % the code accomplishes no change to the image
isequal(Pix,allpixindices) % the index arrays are identical
ans = logical
1
isequal(double(A_Pix),outpict) % the images are identical
ans = logical
1
I'm assuming most people don't need help writing an image processing script that accompishes exactly zero alteration of image data, so you must not be after the same thing that the OP wanted. To create a new image from an old image is perhaps one of the most vague descriptions of image processing that could be conceived. Nobody can give you bespoke code to do a specific task if the task cannot be described.
If your goal is to merely reshape the image into a list of pixel tuples, then just use reshape() and permute() as necessary to get whatever orientation you want.
pixtable = reshape(inpict,[],size(inpict,3))
pixtable = 100×3
62 29 64 63 31 62 65 29 60 63 29 62 63 31 62 63 32 61 62 30 63 65 29 65 62 30 66 61 29 61

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by