Histogram not displaying properly on image
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sorry for the lengthy code, but I have a project in which I have to sort out four different colored apples. To start, we need to generate histograms. I was assigned the yellow apple (#4) shown in the lineup below which I show my code for at the bottom. The histograms for apple 1 and apple 2 work perfectly, yet mine does not generate a histogram at all. Any help would be appreciated.

%%Display four apples
clc;clear all;close all;
reda = imread('redapple.jpg');
dreda = imread('darkredapple.jpg');
greena = imread('greenapple.jpg');
yellowa = imread('yellowapple.jpg');
b=[reda dreda greena yellowa];
imshow(b);
%%Question 2: Apple 1
apple1=b(151:361,71:369,:);
rr=apple1(:,:,1);
gr=apple1(:,:,2);
br=apple1(:,:,3);
figure;
histogram(rr,'facecolor','red');
title('Cortland');
grid
hold on;
histogram(gr,'facecolor','green');
histogram(br,'facecolor','blue');
axes('pos',[.7 .7 .2 .2]);
imshow('redapple.jpg');
hold off;
%%Question 2: Apple 2
apple2=b(97:437,670:870,:);
rrd=apple2(:,:,1);
grd=apple2(:,:,2);
brd=apple2(:,:,3);
figure;
histogram(rrd,'facecolor','red');
grid
title('Red Delicious');
hold on;
histogram(grd,'facecolor','green');
histogram(brd,'facecolor','blue');
axes('pos',[.7 .7 .2 .2]);
imshow('darkredapple.jpg');
hold off;
%%Question 2: Apple 4
apple4=b(410:1630,134:1881,:);
ry=apple4(:,:,1);
gy=apple4(:,:,2);
by=apple4(:,:,3);
figure;
histogram(ry,'facecolor','red');
title('Yellow');
grid
hold on;
histogram(gy,'facecolor','green');
histogram(by,'facecolor','blue');
axes('pos',[.7 .7 .2 .2]);
imshow('yellowapple.jpg');
hold off;
5 Kommentare
Antworten (1)
Image Analyst
am 8 Dez. 2017
I made some minor modifications in your code. You were kind of getting confuesd with the filenames and variable names. I also added some code to get the mean RGB of one of the apples.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%%Display four apples
% clc;clear all;close all;
redApple = imread('redapple.jpg');
darkRedApple = imread('darkredapple.jpg');
greenApple = imread('greenapple.jpg');
yellowApple = imread('yellowapple.jpg');
rgbImage=[redApple, darkRedApple, greenApple, yellowApple];
imshow(rgbImage);
axis on;
h = impixelinfo(); % Lets you mouse around aover image and see RGB values.
%%Question 2: Apple 1
rr=redApple(:,:,1);
gr=redApple(:,:,2);
br=redApple(:,:,3);
figure;
histogram(rr,'facecolor','red');
title('Cortland');
grid
hold on;
histogram(gr,'facecolor','green');
histogram(br,'facecolor','blue');
axes('pos',[.7 .7 .2 .2]);
imshow(redApple);
hold off;
%%Question 2: Apple 2
rrd=darkRedApple(:,:,1);
grd=darkRedApple(:,:,2);
brd=darkRedApple(:,:,3);
figure;
histogram(rrd,'facecolor','red');
grid
title('Red Delicious');
hold on;
histogram(grd,'facecolor','green');
histogram(brd,'facecolor','blue');
axes('pos',[.7 .7 .2 .2]);
imshow(darkRedApple);
hold off;
%%Question 2: Apple 4
ry=yellowApple(:,:,1);
gy=yellowApple(:,:,2);
by=yellowApple(:,:,3);
figure;
histogram(ry,'facecolor','red');
title('Yellow');
grid
hold on;
histogram(gy,'facecolor','green');
histogram(by,'facecolor','blue');
axes('pos',[.7 .7 .2 .2]);
imshow(yellowApple);
hold off;
% Find the mask for the colored part. Colored part will have a saturation > 0.1
hsvImage = rgb2hsv(redApple);
saturationImage = hsvImage(:,:,2); % Extract saturation image.
threshold = 0.4;
mask = saturationImage > threshold;
% Fill holes, get the convex hull, and extract only the largest blob.
mask = imfill(mask, 'holes');
mask = bwconvhull(mask);
mask = bwareafilt(mask, 1);
figure;
imshow(mask);
title('Mask', 'fontSize', 20);
% Get the mean R, G, and B
meanRed = mean(rr(mask))
meanGreen = mean(gr(mask))
meanBlue = mean(br(mask))

What you should do is to take the 3 chunks of code and build it into just one chunk of code that is a function which takes the RGB image variable, plots/displays it, and computes and returns the mean RGB values. Then once you train it with this so you know the mean RGB values for each type, then if you process any known value you can find the distance from the unknown color to the 4 reference colors. Whichever has the smallest distance is the type of apple that it is.
0 Kommentare
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






