Segment structure using threshold
3 views (last 30 days)
Show older comments
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 Comments
Gopichandh Danala
on 10 Apr 2018
Edited: Gopichandh Danala
on 10 Apr 2018
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')
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')
Devika D
on 10 Apr 2018
Edited: Devika D
on 10 Apr 2018
sir,
Actually I am trying to capture the segmented structure of the image.
from the figure if 'a' is the actual image matrix,then b the 5 thresholds.after applying the threshold I must get as in the figure that I have already shown above. hope you got my idea.
Answers (1)
Gopichandh Danala
on 10 Apr 2018
Edited: Gopichandh Danala
on 10 Apr 2018
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
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')
2 Comments
Gopichandh Danala
on 23 Apr 2018
If this answer solves your problem please accept the answer so others find it useful
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!