how can i set an images pixels to black ?

21 ビュー (過去 30 日間)
Alprcnkt
Alprcnkt 2016 年 4 月 2 日
コメント済み: li yan 2016 年 4 月 3 日
hi guys , i m so new with Matlab and have a problem.
i have an image and i have to set all pixels to the 'black' which is greater then 0.5(or true it says) and without using loops. but repmat
can anyone help me ?

回答 (2 件)

li yan
li yan 2016 年 4 月 2 日
I(I > 0.5) = 0;
  3 件のコメント
Walter Roberson
Walter Roberson 2016 年 4 月 2 日
li yan
li yan 2016 年 4 月 3 日
Assuming that you have got a 2D-metrix I, and you want to set all pixels greater than 0.5 to 0. you can do like this
row = 10; col = 10;
I = 0.5 + .1 * randn(row, col);
I(I > 0.5) = 0;
then you have set all pixels greater than 0.5 to 0 in I.

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


Image Analyst
Image Analyst 2016 年 4 月 2 日
You would not use repmat() - that's not appropriate. I don't even know how it could be used to do this. See if this explains it
% Display original image
subplot(2,2,1);
imshow(grayScaleImage, []);
% Get a "mask" of what pixels are brighter than 0.5
% Wherever the image is >0.5 mask will be true/1/white.
% Wherever the image is <= 0.5 the mask will be false/0/black.
mask = grayScaleImage > 0.5;
subplot(2,2,2);
imshow(mask);
title('Binary Image', 'FontSize', 20);
% Now everywhere that mask is true, set the original image to zero there.
grayScaleImage(mask) = 0;
% Display the result.
subplot(2,2,3);
imshow(grayScaleImage);
title('New Image', 'FontSize', 20);
  2 件のコメント
Alprcnkt
Alprcnkt 2016 年 4 月 2 日
thank you so much i also found something like that with help and it worked
green_c = lena_image(:,:,2); % to reaching green channels
black = green_c >= 0.5; % creating a matrix where green >= 0.5
black = repmat(black, [ 1 1 3]); %repeating the black matrix,
lena_image(black)=0;
imshow(lena_image) % showing picture how it supposed to be
Image Analyst
Image Analyst 2016 年 4 月 2 日
You didn't say you were going to mask a color image according to if the green channel was greater than 0.5. To do that you'd normally do this:
mask = rgbImage(:,:,2) > 0.5; % Mask determined from green channel
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
And no repmat needed.

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

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by