You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How I can change the color of a small part of a white border around an image?
12 views (last 30 days)
Show older comments
Hi everyone!
I have a stimulus which is a mosaic of image (a matrix of 4*7). I created a white border around the image, But I want to change the color of border into black only in the middle bottom of the border. This postion is where the participant seat in front of the image projecting on the wall, so this part of the border must be in black to hide the shaddow of the participant.
I would be thankful if any body could let me know if this can basically be done? and how?
An example of the stimulus is attached for better clarification.
Thanks!
16 Comments
Rik
on 22 Feb 2021
You don't have a matrix of 4x7, at most you have a cell array of 4x7. Terminology matters if you want effective help.
What is your exact input and what is the output you would like. This sounds like you can give an easy example by editing the ouput with an external tool like paint to match your expectations. That will make it easier to help you.
KALYAN ACHARJYA
on 22 Feb 2021
Hello, are you referring to the small image or the full mosaic image?
Parishad Bromandnia
on 22 Feb 2021
I will read the links.
I am actually, using the below code after I make one single mosaic of images, to create a white border around it.
In the attached image the part that I want to has black border is shown by using pen from "snipingtool" the rest of the border as you see should stay white.
Thanks!
I = imread('Trial_with_margin.jpg'); %Open image
I2 = addborder(I,90,[255 255 255],'outer'); %Creating white border around whole mosaic
imwrite(I2, 'Trial_with_margin.jpg');
Parishad Bromandnia
on 22 Feb 2021
I just attached a new image showing the part I want to be black. Its around the whole mosaic.
Parishad Bromandnia
on 22 Feb 2021
Edited: Parishad Bromandnia
on 22 Feb 2021
Yes, it has to be like that.
I project this images on the wall (height of projection space:123, lenght of projection space: 170). Participant seats in front of this projection space while the image is projecting via a projector from the back of participant. So the black part in the middle is considered to be without any image (i.e., only blank black space) to hide the shadow of head and part of participant body. I also need white border around the whole mosaic in each trial because I will place April tags around the projection space (to detect head movemnts on the projection space) and the white border helps the tags to be detected, but that small part of border has to be in black. You can see an example of the setting attached (the mosaic is on white background here and the current stimuli must be on black background as I shared a trial of it with you).
Thank you so much!
Rik
on 22 Feb 2021
If you look closely at the image in this comment, you will notice that the area below the center image is not black, but dark grey. If that is indeed the input image, it would be easy to detect the columns where that color occurs and make the bottom rows black or grey as well in those columns. The point is that you need a robust way to detect which columns should be made grey or black.
Parishad Bromandnia
on 22 Feb 2021
Edited: Parishad Bromandnia
on 22 Feb 2021
I see know. Those are just two tiles of blank black images, maybe not black enough :)
The mosaic code goes through a folder of 28 images (each image is one tile of the mosaic) and read them in order.
At the end I have a mosaic of images consisting of 28 tiles (4*7) . So that black area is basically two tiles of blank images.
I could not understood what you mean with making bottom rows black? I once tried to define a mosaic of 6*9 tiles and fill the first and last column as well as first and last rows with blank white images, but this does not work in our setting as the margin is then bigger that what I need.
KALYAN ACHARJYA
on 22 Feb 2021
@Parishad Bromandnia Sorry, honestly I don't understand the question, otherwise, I love to solve such problems.
Rik
on 22 Feb 2021
I didn't mean the rows of pictures, I meant the actual image itself: your image is an array of pixels and you want to make some of them black. Since you refuse to provide specifications for the input image it is very hard to help you. In your photo of the setup the image is completely different from what you uploaded before that. If you don't explain what image to expect, I will not be able to help you.
Parishad Bromandnia
on 22 Feb 2021
Edited: Parishad Bromandnia
on 22 Feb 2021
Do you mean the code for mosaic with specification.
I use the below code and user-defined function to open 28 images in form of a mosaic:
function [Img_layout , Images_pixels] = Mosaic(NRows,NCols,pixels,ShowFlag)
number_of_Rows_in = NRows; %Variable to store number of rows within mosaic
number_of_Cols_in = NCols; %Variable to store number of cols within mosaic
ShoFlg = ShowFlag; %Should be on FALSE
pixels_across_mosaic = pixels; %Variable to store row pixels
%The below code is to make sure if the remainder of devision of the pixels
%across columns and rows/number of columns and rows is equal to 0...
%(i.e., to be sure whether each tile has equal size to the other tiles)
if (mod(pixels_across_mosaic, number_of_Rows_in) == 0)
ReSizeTodefinedDimension = 0;
else
ReSizeToDefinedDimension = 1;
end
Rsized_Rows = floor(pixels_across_mosaic/number_of_Rows_in); %To define tiles with same pixels in rows & columns
Rsized_Cols = Rsized_Rows; %To get same pixels for both rows & columns
%Loading images from the directory
jpg_files = dir(fullfile([pwd '\images'],'*.jpg')); %Change dir to image file
if (size(jpg_files, 1) < 1) %Check if there are .jpg files in folder
disp('No image was found');
return;
end
%Still unclear
fileJPG_path=[pwd '\images\' jpg_files(1).name];
test_i=imread(fileJPG_path);
if (size(test_i,3)>2)
Color_Image=1;
else
Color_Image=0;
end
%Loop over all images
for k = 1:1:length(jpg_files)
fileJPG_path = [pwd '\images\' jpg_files(k).name]; %Go through each image as one index
i = imread(fileJPG_path); %import images in MATLAB
Images_pixels(k).imageData = imresize(i,[Rsized_Rows Rsized_Cols]); %resize one by one
end
%Store new values of row*col struct of images in two variables
New_col_value = Rsized_Cols;
New_Row_value = Rsized_Rows;
%set the images into one mosaic
k = 0;
for i = 0:number_of_Cols_in - 1 %Define a first index for rows
for j = 0:number_of_Rows_in - 1 %Define a second index for columns
k = k+1;
if Color_Image == 0
Img_layout(((j*New_Row_value)+1):((j*New_Row_value)+ New_Row_value),((i*New_col_value)+1):((i* New_col_value)+ New_col_value)) = Images_pixels(k).imageData;
else
Img_layout(((j*New_Row_value)+1):((j*New_Row_value)+ New_Row_value), ((i*New_col_value)+1):((i*New_col_value)+ New_col_value),1) = Images_pixels(k).imageData(:,:,1);
Img_layout(((j*New_Row_value)+1):((j*New_Row_value)+ New_Row_value), ((i*New_col_value)+1):((i*New_col_value)+ New_col_value),2) = Images_pixels(k).imageData(:,:,2);
Img_layout(((j*New_Row_value)+1):((j*New_Row_value)+ New_Row_value), ((i*New_col_value)+1):((i*New_col_value)+ New_col_value),3) = Images_pixels(k).imageData(:,:,3);
end
end
end
%Final representation of the mosaic of images into one figure
if ShoFlg == 1
if ReSizeToDefinedDimension == 1
disp('Not divisible');
figure;
imshow(imresize(Img_layout,pixels_across_mosaic), border ,'loose');
else
figure;
imshow(Img_layout);
end
end
Parishad Bromandnia
on 22 Feb 2021
%% Refresh the workspace and command window
close all;
clear;
clc;
%% Creating a "dialog box" which enable the user of the code to interact with MATLAB
%When the code runs and the dlg box is open, the user must enter 3 inputs:
prompt = {'Enter Number of Images in Rows:','Enter Number of Images in Cols:','Enter Range of pixels'};
%1.Number of Images in Rows
%2.Number of Images in Cols
%3.Number of pixels across rows/columns
dlg_title = 'Input for Image Tiling'; %Giving the Dialog box a title in form of char array
num_lines = 1; %Defining how many lines each input should have
def = {'4','7','2000'}; %Here is the pre-defined input
answer = inputdlg(prompt,dlg_title,num_lines,def);
%Storing all inputs(i.e., results from dlg box)in one cell
%% Generating the Images by using the User-defined Function so-called "Mosaic_Of_image"
%The function: [Img_layout , Images_pixels] = Mosaic(NRows,NCols,pixels,ShowFlag)
[Img_layout , Images_pixels] = Mosaic(str2double(answer{1}),str2double(answer{2}),str2double(answer{3}),0);
%The 3 inputs from dlg box must be entered to the function inputs
%Note: the inputs are string values, so they must be changed to numerical
%values with str2num function
figure; %Opening an empty figure
imshow(Img_layout); %Display the output(i.e., the mosaic)in a figure
u_in = mat2gray(Img_layout); %Set the output(i.e.,figure)values on minimum
imwrite(RemoveWhiteSpace(u_in),'Trial_with_margin.jpg'); %To delete the white margin around the figure when saving
%end
Parishad Bromandnia
on 22 Feb 2021
Edited: Parishad Bromandnia
on 22 Feb 2021
@Rik Before I run the above code, I creat borders of different sizes for each image (i.e., each tile) with the code I sent you before. By combining these images with black borders of different sizes together, at the end I have the mosaic with black background and some images are bigger / some smaller depending of the size of black border aound them. I hope I am clear enough.
Parishad Bromandnia
on 22 Feb 2021
Edited: Parishad Bromandnia
on 22 Feb 2021
Dear @KALYAN ACHARJYA, I tried to show what I want by using paint. I tried to be as specific as possible. Maybe what I want should not be done by defining border around image, but this was the only solution came to my mind so far.
KALYAN ACHARJYA
on 22 Feb 2021
Edited: KALYAN ACHARJYA
on 22 Feb 2021
@Parishad Bromandnia From the input attached image, it can be manipulated directly? If I read the attached input image and make a black color in the particular area, is there an issue?
Parishad Bromandnia
on 22 Feb 2021
@KALYAN ACHARJYA, Exactly, I meant that. I just do not know how to make that particular area black. Please find the original stimulus attached, You can directly read it in MATLAB: Please let me know of potential solution.
Accepted Answer
KALYAN ACHARJYA
on 22 Feb 2021
rgbImage=imread('Trial_with_margin.jpg');
grayImage=rgb2gray(rgbImage);
[r,~]=find(grayImage==0);
max_row=max(r);
mid_col=round(size(rgbImage,2)/2);
% I have directly set here, it can be generalized
th=300; % Find the coloumns of the cat image
rgbImage(max_row:end,mid_col-th:mid_col+th,1:3)=0;
imshow(rgbImage);
4 Comments
Parishad Bromandnia
on 22 Feb 2021
@KALYAN ACHARJYA, Thank you so much! That is exactly what I meant.
Thank you again.
KALYAN ACHARJYA
on 22 Feb 2021
Edited: KALYAN ACHARJYA
on 22 Feb 2021
OMG, @Parishad Bromandnia you wrote such a long text, I was afraid to read it completely. I thought it could be some big issues. @Rik also questioned you on several points. Whatever it is, glad to know that the problem has been resolved.
Cheers!
Kalyan :)
Parishad Bromandnia
on 22 Feb 2021
@KALYAN ACHARJYA, Right, it was my first question and maybe I could not be clear enough :)
Anyway the code is exactly what I needed.
Thanks again :)
All the best,
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)