creating a 2D circle with fixed hue, saturation and luminance and applying a gaussian filter

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

By the way, here how I solved this (with Image Analyst help and patience):
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.
% Create a logical image of a circle
rows = 280;
columns = 280;
[columnsInImage, rowsInImage] = meshgrid(1:columns, 1:rows);
centerX = 140;
centerY = 140;
radius = 125;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
image(circlePixels) ;
axis('off', 'image');
colormap([0 0 0; 1 1 1]);
% loop to create 51 circles 5 degrees step, L 85 S 38
for hueAngle = 0 : 5 : 250
tic;
hueChannel = zeros(rows, columns);
hueChannel(circlePixels) = hueAngle / 360; % Normally goes from 0-1 which is 0-360 degrees.
saturationChannel = zeros(rows, columns);
saturationChannel(circlePixels) = 38/100;
valueChannel = zeros(rows, columns);
valueChannel(circlePixels) = 85/100;
hsvImage = cat(3, hueChannel, saturationChannel, valueChannel);
rgbImage = hsv2rgb(hsvImage);
imwrite(rgbImage,sprintf('circle_%d.png', hueAngle))
if hueAngle == 0
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
end
end
% apply low pass gaussian filter, 10 SD
files = dir('C:\Users\andre\OneDrive\Desktop\prove_matlab\stimuli\*.png');
for k = 1 : length(files)
thisName = files(k).name
fullFileName = fullfile('C:\Users\andre\OneDrive\Desktop\prove_matlab\stimuli\', files(k).name)
theImage = imread(fullFileName);
subplot(1, 2, 1);
imshow(theImage);
drawnow;
blurredImage = imgaussfilt(theImage, 10);
subplot(1, 2, 2);
imshow(blurredImage);
drawnow;
outputBaseFileName = sprintf('blur_%s', thisName);
outputFullFileName = fullfile('C:\Users\andre\OneDrive\Desktop\prove_matlab\stimuli\', outputBaseFileName)
imwrite(blurredImage, outputFullFileName);
end

Weitere Antworten (2)

See if the attached demo helps you. If not, write back.

4 Kommentare

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
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.
If you're referring to the code I wrote in my question.. yes, I made it (of course I had a look around in the internet to make it). If your question is if I need this code to do an assignment: nope. I'm a researcher and I need it to create image stimuli for an experimental task I will use for a research.
Cheers

Melden Sie sich an, um zu kommentieren.

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

That's awesome! Thank you.
I had a look to the code, and I have a couple of questions:
1 - is cielab space different from hsv space? because in the code it seems to me you are using hsv and rgb spaces, not cielab one. Am I wrong? I need the hue changes (from 0, red to 250, blue) of all the circles to be referred to the cielab space, not only the first circle to be in cielab and the others in hsv/rgb
2 - the circle I have after the filter and all consecutive ones don't seem to have luminance 85/100, but the first one (the white circle) probably do. why does that happen? how can I fix those values?
3 - is there a way to save (after all the transformation I need) each circle as a different png image without the black baground? I know matlab doesn't manage transparency, but I guess if we work on the transparency channel of the background it will probably result in a transparent background once saved. am I correct?
4 - the low pass gaussian filter is somehow different from what I expected. I have some stimuli samples (that are exactly what I expect after all the transformations) taken from a study that I have to replicate. they said they used "a Gaussian low-pass filter of size 100 x 100 with a standard deviation of 10" to blur the circles, but their result is somehow different from mine. do you know why? I attached few sample stimuli. The last one is what I have using your code, the first two images come from the original study.
Sorry for all those questions, and thank for your help so far!
Oh I got now that your code applies gaussian filter to saturation, hue and luminance (correct me if I'm wrong).. actually I need the low-pass gaussian filter to be applied over the image shape to blur it. Lum, sat and hue should be without any filter!
I probably didn't explain that clearly..
Thanks!
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.
thanks. and what about the other questions?
by the way, I tried changing some stuff in your code to understand what it does properly, specifically
% 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;
it seems to me that those three channels are not hue, saturation and luminance.. but red, green and blue. so we are in RGB space, not cielab?
I would like to understand how to work in cielab coordinates and how to make a loop to change the hue from 0 to 250 with 5 degrees steps, and to save each step as a png file without any background (only the circle). don't worry about the gaussian filter, I can apply it after that and I guess I understood how to do it on the whole image.
Thanks in advance
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.
OK, got it. thanks for your patience! Just a few quick questions so I can understand how to proceed: since I need the circles to have the same exact colour on all the devices I use, which colour space do you suggest me to use? I know that cielab is an international standard, while RGB is device-dependant. what about HSL/HSV spaces? I mean, if I change HSV hue of 5 degrees on my laptop, will I have the exact same colour on another screen?
Last question: if I don't want any blur, and I want only to save as an image each circle created in a different hsv hue angle, what do I have to change in your code?
Thanks
Andrea
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.

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by