Looping each pixel of matrix image

83 ビュー (過去 30 日間)
Steven Pranata
Steven Pranata 2019 年 12 月 9 日
コメント済み: DGM 2022 年 11 月 2 日
I have image 250x100 and i want using pdf every each pixel... How to looping it? If its manual just be: X = imread('img.jpg'); Val1 = pdf(X(1,1),mu,sigma); Val2 = pdf(X(1,2),mu,sigma);
  2 件のコメント
Image Analyst
Image Analyst 2019 年 12 月 15 日
Steven, why are you flagging your own post as spam? Show respect to Bhaskar and put back your original post.
Stephen23
Stephen23 2019 年 12 月 15 日
Steven Pranata's original question (from Google Cache):
"Looping each pixel of matrix image"
I have image 250x100 and i want using pdf every each pixel... How to looping it? If its manual just be: X = imread('img.jpg'); Val1 = pdf(X(1,1),mu,sigma); Val2 = pdf(X(1,2),mu,sigma); ......

サインインしてコメントする。

回答 (1 件)

Bhaskar R
Bhaskar R 2019 年 12 月 9 日
If input image is color image
X = imread('img.jpg');
Val = zeros(numel(X)); % initialize pdf values storing variable with zeros
k = 1; % counter variable in for loop
for cha = 1:size(X, 3) % loop of (3 RGB colors)
for r = 1:size(X, 1) % for number of rows of the image
for c = = 1:size(X, 2) % for number of columns of the image
Val(k) = pdf(X(r, c, cha),mu,sigma);
k = k+1; % increment counter loop
end
end
end
If the input image is grayscale image
for r = 1:size(X, 1) % for number of rows of the image
for c = = 1:size(X, 2) % for number of columns of the image
Val(k) = pdf(X(r, c),mu,sigma);
k = k+1; % increment counter loop
end
end
  2 件のコメント
User79
User79 2022 年 11 月 1 日
Can you run your code before publishing, please?
DGM
DGM 2022 年 11 月 2 日
I find it unsurprising that a hit & run OP accepted non-functioning code.
Obviously,
Val = zeros(numel(X));
will create an array that's not the same size as X. It will almost certainly create memory usage issues or errors.
Also, the use of accumulated linear indexing within a set of subscript loops is dependent on the loop order. That's one good reason to avoid it. This is a perfect example. The output image is striped because the indexing is wrong. If you're writing to the same size array in a set of subscript loops, use subscript indexing.
The distribution parameters are undefined and the syntax for pdf() is wrong. It's also worth noting that pdf() will throw misleading errors if it's fed integer-class image data like this.
Assuming that pdf() is not some user-defined function by the same name, there's no point using pdf() in a loop like this at all. The output of pdf() is the value of the specified PDF, as evaluated at the points specified by X. The way this is written isn't a spatial filter. Each output pixel would be the probability of that pixel value, given the PDF of pixel values. I don't know what purpose that would serve, but you could do the same thing without the loop. The following example would recreate what the above code would do if it were fixed.
% an example image and distribution parameters
X = imread('peppers.png');
mu = 128;
sigma = 100;
dname = 'norm';
% calculate probability of each pixel value
Y = pdf(dname,double(X),mu,sigma);
% these are all pretty small
[min(Y(:)) max(Y(:))]
ans = 1×2
0.0018 0.0040
% so normalize for viewing
% note that this isn't a spatial filter at all
imshow(normalize(Y,'range'))
There are plenty of examples for how to write a basic image filter.

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by