creating a 2D circle with fixed hue, saturation and luminance and applying a gaussian filter
Ältere Kommentare anzeigen
Hi,
I'm quite new to Matlab, so that's probably a dumb question. Anyway.. I'd appreciate any help.
I basically need to understand how:
1) to create a filled circle image (without background) in cielab space. The circle should have a r=125 pixels and fixed saturation (38) and luminance (85) values. the hue should be of 0 in cielab space
2) to apply a Gaussian low-pass filter of size 100 x 100 with a standard deviation of 10 only to the circle
3) to create a loop to replicate this operation, in order to save different circle images with same luminance and saturation, and different hue degrees in steps of 5 degrees each. in particular I would need the first circle to be 0 hue degrees, the second to be 5, the third to be 10 and so on - until they reach 250 degrees. so at the end I should have 51 circles with slightly different colours.
I had a look online and I understood - correct me if I'm wrong - that Matlab isn't able to manage image transparency. what would you suggest? After having those images, I would basically need to superimpose them on a black screen, so it would probably be a solution to save the final filtered image with a black background - but I have to be sure they will be reliable because they will be part of a behavioural visual experiment, and there is no room for errors.
below some code I used to create the filtered circle, but I'm not able to export it in cielab space and with no (or black) background. Also, I don't know how to create a loop!
% create a red circle.
set(gca,'Color','black')
cerchio_fig = rectangle('Position',[1,1,5,5],...
'Curvature',[1,1], 'FaceColor','r')
axis equal off;
saveas (cerchio_fig,'cerchio.png')
%%create a gaussian filter and save as a tiff
cerchio = imread('cerchio.png')
set(gcf,'Color','black')
cerchioblur = imgaussfilt(cerchio,10)
imwrite (cerchioblur,'cerchioblur.tiff')
%%imwrite (cerchioblur,'cerchioblur.tiff','Colorspace','cielab') %%when I
%%do that, my circle will be saved with completely different colours and
%%when opened back in matlab it will be converted to a different colour
%%space
imshow (cerchioblur)
Any suggestion?
Thanks in advance
Akzeptierte Antwort
Weitere Antworten (2)
Image Analyst
am 22 Sep. 2018
0 Stimmen
See if the attached demo helps you. If not, write back.

4 Kommentare
Image Analyst
am 22 Sep. 2018
Andreas's "Answer" moved here since it's a comment to me rather than an "Answer" to her original question.
thanks for your response.. but I don't understand how to use that cose. this code gives me several windows with rgb hue, luminance and saturation.. my question was a bit different - or I've been unclear. I need basically to create 51 filled circles with a low pass gaussian filter (100x100, 10 sd) applied. the circles should be from 0 to 250 hue degrees, in steps of 5 degrees.
how can I do that? I'm seriously stuck..
Thanks
Image Analyst
am 22 Sep. 2018
I was hoping that you'd be able to adapt it.
Is this your homework (it sounds like it). If so let me know because we need to make sure you don't turn someone else's code in as your own or you could get into trouble.
Andrea C
am 22 Sep. 2018
Image Analyst
am 22 Sep. 2018
OK. I'll do it for you.
Image Analyst
am 22 Sep. 2018
Andrea, see code below. The colors will be more vivid if you instead the saturation value or brightness value.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 22;
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
rows = 480;
columns = 640;
[columnsInImage, rowsInImage] = meshgrid(1:columns, 1:rows);
% Next create the circle in the image.
centerX = 320;
centerY = 240;
radius = 125;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
image(circlePixels) ;
axis('on', 'image');
colormap([0 0 0; 1 1 1]);
title('Binary image of a circle');
% 1) to create a filled circle image (without background) in cielab space.
% The circle should have a r=125 pixels and fixed saturation (38) and luminance (85) values.
% The hue should be of 0 in cielab space
hueChannel = zeros(rows, columns);
saturationChannel = zeros(rows, columns);
saturationChannel(circlePixels) = 38;
valueChannel = zeros(rows, columns);
valueChannel(circlePixels) = 85;
% 2) to apply a Gaussian low-pass filter of size 100 x 100
% with a standard deviation of 10 only to the circle
hueChannel = imgaussfilt(hueChannel, 2, 'FilterSize', 101) / 255;
saturationChannel = imgaussfilt(saturationChannel, 2, 'FilterSize', 101) / 255;
valueChannel = imgaussfilt(valueChannel, 2, 'FilterSize', 101) / 255;
% Create hsv image
hsvImage = cat(3, hueChannel, saturationChannel, valueChannel);
% Convert back to RGB
rgbImage = hsv2rgb(hsvImage);
% Display image
imshow(rgbImage);
axis on;
% 3) to create a loop to replicate this operation,
% in order to save different circle images with same luminance and saturation,
% and different hue degrees in steps of 5 degrees each.
% in particular I would need the first circle to be 0 hue degrees,
% the second to be 5, the third to be 10 and so on - until they reach 250 degrees.
% so at the end I should have 51 circles with slightly different colours.
figure;
plotNumber = 1;
for hueAngle = 0 : 5 : 250
tic;
hueChannel = zeros(rows, columns);
hueChannel(circlePixels) = hueAngle / 360; % Normally goes from 0-1 which is 0-360 degrees.
% Blur it.
hueChannel = imgaussfilt(hueChannel, 2, 'FilterSize', 101);
fprintf('Hue(240, 320) = %f\n', hueChannel(240, 320));
% Create hsv image
hsvImage = cat(3, hueChannel, saturationChannel, valueChannel);
% Convert back to RGB
rgbImage = hsv2rgb(hsvImage);
% Display image
subplot(7, 8, plotNumber);
imshow(rgbImage);
caption = sprintf('Hue Angle = %d', hueAngle);
title(caption);
axis on;
drawnow;
impixelinfo;
plotNumber = plotNumber + 1;
toc
if hueAngle == 0
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
end
end

8 Kommentare
Image Analyst
am 23 Sep. 2018
That's correct - you didn't. If you don't filter any of the channels (lum, sat, or hue), then you're not blurring the image. It's impossible to blur it without changing one or more of those channels. Usually however, people will blur only the luminance channel (L or V) rather than all 3. Otherwise you can end up with color artifacts.
Andrea C
am 23 Sep. 2018
Image Analyst
am 23 Sep. 2018
You cannot change saturation in RGB color space or CIELAB color space, at least not easily. You can only change it in HSV color space. That's why I created the images initially in HSV color space. Since you want specifically defined saturation, luminance, and hue values, you must do this in HSV color space, then convert back to RGB color space for display. If you were to do it in CIELAB color space, you'd basically have to convert to HSV color space anyway before you can make any assignments, so it's of no help whatsoever to start in CIELAB color space, in fact it makes it more difficult. I mean, if you were to create a CIELAB image first, what would you assign for the values, given that the values are in HSV coordinates? You'd have no idea. YOu'd have to start in HSV color space to even know what values to use in LAB color space. So that's why we start in HSV color space at the beginning.
As far as blurring goes, to avoid color artifacts at edges, you need to blur only the lightness (value) channel. Any blurring in any other color space channel could introduce color (hue) artifacts (changes) around edges.
Andrea C
am 24 Sep. 2018
Image Analyst
am 25 Sep. 2018
Then you absolutely MUST color calibrate to your devices. See this page: https://www.xrite.com/categories. See the categories "Color calibration and Profiling" and "Color Matching Apps" to learn how you can calibrate your software to your screens to achieve what you said.
Kategorien
Mehr zu Blue finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


