poster
2019-12-10, 20:30
I = imread('coins.png'); level = graythresh(I); BW = im2bw(I,level); imshow(BW) 上面是MATLAB文档中使用灰度图像的示例。如何使它与索引图像(如https://i.stack.imgur.com/OsccS.png (https://i.stack.imgur.com/OsccS.png)在这篇文章中 (https://stackoverflow.com/questions/2586539/why-do-i-get-an-all-black-grayscale-image-when-i-load-this-png-in-matlab) ?
回答:
您可以先使用IND2GRAY (http://www.mathworks.com/access/helpdesk/help/toolbox/images/ind2gray.html)函数将索引图像及其色彩 (http://www.mathworks.com/access/helpdesk/help/toolbox/images/ind2gray.html)图转换为灰度图像:
[X,map] = imread('SecCode.php.png'); %# Read the indexed image and colormap grayImage = ind2gray(X,map); %# Convert to grayscale image 然后,您可以应用上面的代码:
level = graythresh(grayImage); %# Compute threshold bwImage = im2bw(grayImage,level); %# Create binary image imshow(bwImage); %# Display image 编辑:
如果您想对任何类型的图像都采用这种通用方法,则可以采用以下一种方法:
%# Read an image file: [X,map] = imread('an_image_file.some_extension'); %# Check what type of image it is and convert to grayscale: if ~isempty(map) %# It's an indexed image if map isn't empty grayImage = ind2gray(X,map); %# Convert the indexed image to grayscale elseif ndims(X) == 3 %# It's an RGB image if X is 3-D grayImage = rgb2gray(X); %# Convert the RGB image to grayscale else %# It's already a grayscale or binary image grayImage = X; end %# Convert to a binary image (if necessary): if islogical(grayImage) %# grayImage is already a binary image bwImage = grayImage; else level = graythresh(grayImage); %# Compute threshold bwImage = im2bw(grayImage,level); %# Create binary image end %# Display image: imshow(bwImage); 除某些离群值(例如TIFF图像的备用颜色空间 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/imread.html#f25-721031) )外,这应涵盖大多数图像类型。
更多&回答... (https://stackoverflow.com/questions/2587110)
回答:
您可以先使用IND2GRAY (http://www.mathworks.com/access/helpdesk/help/toolbox/images/ind2gray.html)函数将索引图像及其色彩 (http://www.mathworks.com/access/helpdesk/help/toolbox/images/ind2gray.html)图转换为灰度图像:
[X,map] = imread('SecCode.php.png'); %# Read the indexed image and colormap grayImage = ind2gray(X,map); %# Convert to grayscale image 然后,您可以应用上面的代码:
level = graythresh(grayImage); %# Compute threshold bwImage = im2bw(grayImage,level); %# Create binary image imshow(bwImage); %# Display image 编辑:
如果您想对任何类型的图像都采用这种通用方法,则可以采用以下一种方法:
%# Read an image file: [X,map] = imread('an_image_file.some_extension'); %# Check what type of image it is and convert to grayscale: if ~isempty(map) %# It's an indexed image if map isn't empty grayImage = ind2gray(X,map); %# Convert the indexed image to grayscale elseif ndims(X) == 3 %# It's an RGB image if X is 3-D grayImage = rgb2gray(X); %# Convert the RGB image to grayscale else %# It's already a grayscale or binary image grayImage = X; end %# Convert to a binary image (if necessary): if islogical(grayImage) %# grayImage is already a binary image bwImage = grayImage; else level = graythresh(grayImage); %# Compute threshold bwImage = im2bw(grayImage,level); %# Create binary image end %# Display image: imshow(bwImage); 除某些离群值(例如TIFF图像的备用颜色空间 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/imread.html#f25-721031) )外,这应涵盖大多数图像类型。
更多&回答... (https://stackoverflow.com/questions/2587110)