Segment structure using threshold
2 次查看(过去 30 天)
显示 更早的评论
I need to extract the texture.for that,first I have set 5 thresholds. For each pixel,checked if they are between any thresholds,then the pixel value is replaced by lower threshold value index of the two.now it is told that they would give a segmented structure of the actual figure.But I not getting,please any help me
4 个评论
Gopichandh Danala
2018-4-10
编辑:Gopichandh Danala
2018-4-10
I am not clear about what you meant by 'extract texture'. I am answering with the assumption that you want to segment either with a single threshold (global threshold) or use a multi-level threshold to segment multiple regions..
img = rgb2gray(imread('image.jpg'));
figure, imshow(img,[])
% global otsu thresholding
level=graythresh(img);
BW=imbinarize(img,level);
figure, imshow(BW,[])
% I am not sure if you are looking for something like this?
num_threshs = 5;
% Set five thresholds..
% (I used Multilevel image thresholds using Otsu’s method )
thresh = multithresh(img,num_threshs);
% Extract five threshold images
img1 = img <= thresh(1);
img2 = and(img <= thresh(2),img > thresh(1));
img3 = and(img <= thresh(3), img > thresh(2));
img4 = and(img <= thresh(4), img > thresh(3));
img5 = and(img <= thresh(5), img > thresh(4));
img6 = img > thresh(5);
figure,
subplot(231), imshow(img1,[]), title('img <= T1')
subplot(232), imshow(img2,[]), title('img > T1 && img <= T2')
subplot(233), imshow(img3,[]), title('img > T2 && img <= T3')
subplot(234), imshow(img4,[]), title('img > T3 && img <= T4')
subplot(235), imshow(img5,[]), title('img > T4 && img <= T5')
subplot(236), imshow(img6,[]), title('img > T5')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/189091/image.png)
Or use imquantize.
quantize_img = imquantize(img,thresh);
figure,
subplot(121), imshow(quantize_img,[]), title('Quantized image')
subplot(122), imshow(label2rgb(quantize_img),[]), title('Quantized color image')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/189094/image.png)
回答(1 个)
Gopichandh Danala
2018-4-10
编辑:Gopichandh Danala
2018-4-10
Quantize does what you are expecting.
It will divide the given image into a number of labels based on thresh values as shown in the above image.
Here I am using multithresh function to obtain five threshold values.
So, I will have 6 possible ranges (i.e. less than <T1, T1-T2, T2-T3, T3-T4, T4-T5,>T5 = 6 possible labels you can adjust this)
img = rgb2gray(imread('image.jpg'));
num_threshs = 5;
thresh = multithresh(img,num_threshs);
quantize_img = imquantize(img,thresh);
figure,
subplot(221), imshow(img,[]), title('orig-image')
subplot(222), imshow(quantize_img,[]), title('Quantized image')
subplot(2,2,[3,4]), histogram(quantize_img), title('Each pixel labelled 1-to-6')
unique(quantize_img)
ans =
1
2
3
4
5
6
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/181213/image.png)
If you want to give the 5 thresholds manually, just create an array yourself and pass it.
manual_thresh = [20, 40, 60, 80 ,100] # change this
quantize_img = imquantize(img,manual_thresh);
If you want to manually do it as shown in above figure
img = rgb2gray(imread('crop1.jpg'));
num_threshs = 5;
%thresh = multithresh(img,num_threshs); [32 52 86 128 173]
thresh = [min(img(:)),52,86,128, max(img(:))]; % to cover full range of img
label_img = zeros(size(img));
label_img(and(img < thresh(2),img >= thresh(1))) = 1;
label_img(and(img < thresh(3),img >= thresh(2))) = 2;
label_img(and(img < thresh(4),img >= thresh(3))) = 3;
label_img(and(img <= thresh(5),img >= thresh(4))) = 4;
figure,
subplot(121), imshow(img,[]), title('orig-image')
subplot(122), imshow(label_img,[]), title('label-image')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/181214/image.png)
2 个评论
Gopichandh Danala
2018-4-23
If this answer solves your problem please accept the answer so others find it useful
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!