# Histogram-based RGB segmentation

조회 수: 7 (최근 30일)
Teshan Rezel . 2020년 2월 6일
댓글: Teshan Rezel . 2020년 2월 7일
Hi All,
I'm trying to segment an RGB image based on it's histograms; my code is as follows:
%Split into RGB Channels
Red = img(:,:,1);
Green = img(:,:,2);
Blue = img(:,:,3);
%Get histValues for each channel
[yRed, x] = imhist(Red);
[yGreen, x] = imhist(Green);
[yBlue, x] = imhist(Blue);
%Plot them together in one plot
plot(x, yRed, 'Red', x, yGreen, 'Green', x, yBlue, 'Blue');
histRGB = hist([imhist(Red), imhist(Green), imhist(Blue)]);
I get the following error when compiling this section: "Array dimensions must match for binary array op." I'm not sure how to fix this, as I'm not well versed in Matlab currently.
Any help will be appreciated!
Thanks

댓글을 달려면 로그인하십시오.

### 채택된 답변

I am not sure about which algorithm you are trying to implement but below is an example of histogram based segmentation using Otsu's method.
clc; close all;
% Read image into the workspace
% Calculate a 255-bin histogram for the image
[counts, ~] = imhist(rgb2gray(img), 255);
% Compute a global threshold using the histogram counts
T = otsuthresh(counts);
% Create a binary image using the computed threshold
BW = imbinarize(rgb2gray(img), T);
% Display the image
figure; imshowpair(img, BW, 'montage');
##### 댓글 수: 1표시 없음숨기기 없음
Teshan Rezel 2020년 2월 7일

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Get Started with Image Processing Toolbox에 대해 자세히 알아보기

R2019b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!