How I can change the color of a small part of a white border around an image?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2021-2-22
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.
Parishad Bromandnia
2021-2-22
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/527499/image.jpeg)
Parishad Bromandnia
2021-2-22
I just attached a new image showing the part I want to be black. Its around the whole mosaic.
Parishad Bromandnia
2021-2-22
编辑:Parishad Bromandnia
2021-2-22
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!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/527529/image.jpeg)
Rik
2021-2-22
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
2021-2-22
编辑:Parishad Bromandnia
2021-2-22
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
2021-2-22
@Parishad Bromandnia Sorry, honestly I don't understand the question, otherwise, I love to solve such problems.
Rik
2021-2-22
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
2021-2-22
编辑:Parishad Bromandnia
2021-2-22
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
2021-2-22
%% 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
2021-2-22
编辑:Parishad Bromandnia
2021-2-22
@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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/527749/image.jpeg)
Parishad Bromandnia
2021-2-22
编辑:Parishad Bromandnia
2021-2-22
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/527809/image.jpeg)
KALYAN ACHARJYA
2021-2-22
编辑:KALYAN ACHARJYA
2021-2-22
@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
2021-2-22
@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.
采纳的回答
KALYAN ACHARJYA
2021-2-22
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/527879/image.png)
4 个评论
Parishad Bromandnia
2021-2-22
@KALYAN ACHARJYA, Thank you so much! That is exactly what I meant.
Thank you again.
KALYAN ACHARJYA
2021-2-22
编辑:KALYAN ACHARJYA
2021-2-22
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
2021-2-22
@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,
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)