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

8 次查看(过去 30 天)
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

采纳的回答

Andrea C
Andrea C 2018-9-25
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

更多回答(2 个)

Image Analyst
Image Analyst 2018-9-22
See if the attached demo helps you. If not, write back.
  4 个评论
Andrea C
Andrea C 2018-9-22
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

请先登录,再进行评论。


Image Analyst
Image Analyst 2018-9-22
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 个评论
Andrea C
Andrea C 2018-9-24
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
Image Analyst
Image Analyst 2018-9-25
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Modify Image Colors 的更多信息

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by