查看单个帖子
旧 2019-12-10, 20:30   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何在MATLAB中的索引图像上使用Graythresh?

I = imread('coins.png'); level = graythresh(I); BW = im2bw(I,level); imshow(BW) 上面是MATLAB文档中使用灰度图像的示例。如何使它与索引图像(如这篇文章中



回答:

您可以先使用IND2GRAY函数将索引图像及其色彩图转换为灰度图像:

[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图像的备用颜色空间 )外,这应涵盖大多数图像类型。



更多&回答...
poster 当前离线   回复时引用此帖