Reputation: 4124
I'm writing a matlab method which get 2 parameters: uint8 image and grey level which all pixels with gray level larger than par_1 are set to white.
How can I show a image after my all actions ?
Here's my code:
function im = function_1(img, par_1)
checkUint8Image = isa(img, 'uint8');
if checkUint8Image
im = uint8(img);
[row, column] = size(im);
im2 = ones(row, column); % white image
for i=1:row
for j=1:column
if(im(i,j) <= par_1)
im2(i,j) = im(i,j);
end
end
end
imshow(im2);
else
disp('im paramter is not a uint8 type');
end
Maybe there is another way to solve this problem.
Input:
Output:
Upvotes: 0
Views: 97
Reputation: 26
Try this code,
function im = function_1(img, par_1)
checkUint8Image = isa(img, 'uint8');
if checkUint8Image
im = uint8(img);
[row, column, dim] = size(im);
im2 = ones(row, column).*255; % white image
for i=1:row
for j=1:column
if(im(i,j) <= par_1)
im2(i,j) = im(i,j);
end
end
end
imshow(uint8(im2));
else
disp('im paramter is not a uint8 type');
end
Here im2 is casted into uint8. And also note the line,
im2 = ones(row, column).*255;
Upvotes: 0
Reputation: 3574
I think you either use a value for par_1
where the whole image get sets to 1, that's why you get a white image, or you're having a scaling issue, that can be solved with the []
argument to imshow
.
Shorter version of your function:
function im = function_1(img, par_1)
validateattributes(img, {'uint8'}, {'2d'});
im=img;
im(im > par_1)=1;
imshow(im, [], 'InitialMagnification', 'fit');
end
Let's test it with your image and a value of 100
(the original image has values between 0 and 255, setting pixels to 1 will make them look black). Let's try it:
Testim=imread('https://i.sstatic.net/uJsPY.png');
function_1(Testim, 100);
Result:
Upvotes: 1