Combine images in matlab with transparency

조회 수: 19 (최근 30일)
Saqib
Saqib 2014년 4월 19일
댓글: Image Analyst 2014년 9월 7일
Hi could anyone among you give me the code on how to combine images
If this is Image(A)
& this is Image(B)
so is it possible to create an image like the one shown below in MATLAB (produced using photoshop) I yes, pease tell me how as i want the appe only to show and the rest to be fully balck or white thanks

채택된 답변

Image Analyst
Image Analyst 2014년 4월 19일
Try this demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\saqib\Documents\Temporary';
baseFileName = 'apple.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
% redChannel = rgbImage(:, :, 1);
% greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display the image.
subplot(2, 3, 2);
imshow(blueChannel);
axis on;
title('Blue Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(blueChannel);
subplot(2, 3, 3);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of blue channel', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Threshold so we can get a binary image to use as a mask.
binaryImage = blueChannel < 100;
% Display the image.
subplot(2, 3, 4);
imshow(binaryImage);
axis on;
title('Binary Image', 'FontSize', fontSize);
% Fill to get rid of holes
binaryImage = imfill(binaryImage, 'holes');
% Get rid of small things
mask = bwareaopen(binaryImage, 1000);
% Display the image.
subplot(2, 3, 5);
imshow(mask);
axis on;
title('Mask Image', 'FontSize', fontSize);
% Mask original image.
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, class(rgbImage)));
% Display the image.
subplot(2, 3, 6);
imshow(maskedRgbImage);
axis on;
title('Masked Color Image', 'FontSize', fontSize);
  댓글 수: 4
Image Analyst
Image Analyst 2014년 4월 19일
Study Steve's post and you'll see how you can define whatever area you want to be transparent. For example, instead of
alpha_data = checkerboard(block_size, P, Q) > 0;
you'd use
alpha_data = mask; % The mask that Image Analyst's code produces.
Just use the apple mask instead of the checkerboard. Does that make sense?
If you want masking, then my code did that, so can you mark it Accepted?
Saqib
Saqib 2014년 4월 19일

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Joseph Cheng
Joseph Cheng 2014년 4월 19일
편집: Joseph Cheng 2014년 4월 19일
If you have the 2nd image (background 0's and apple areas 1) lets call that MASK, and let's call the apple image APPLE.
JustApple = zeros(size(APPLE));
JustApple(MASK) = APPLE(MASK);
if MASK is type logical then above will work. If MASK is not logical and just a matrix of 1s and 0s then JustApple(logical(MASK)) = APPLE(logical(MASK));
  댓글 수: 3
Image Analyst
Image Analyst 2014년 4월 20일
His code assumes you have the mask already . My code shows you how to get the mask. It displays the mask for you to see in the lower middle, and it also gets the "JustApple" image as you can see from the lower right image in my screenshot.
Saqib
Saqib 2014년 4월 20일
thanks & i will use your code oly

댓글을 달려면 로그인하십시오.


Saqib
Saqib 2014년 4월 19일

Horia
Horia 2014년 8월 10일
편집: Horia 2014년 9월 7일
#
Suppose:
# A=Name1.bmp;
J = imread(A);
I=rgb2gray(J);
[sz1,sz2]=size(I);
K=zeros(sz1,sz2);
for a=1:2:sz1
for b=1:2:sz2
M=I(a:a+1,b:b+1);
if sum(M(:))>=1000 %I chose white, but you may choose whatever color you want
K(a:a+1,b:b+1)=255;
end
end
end
set(gcf,'Color','None')
# B=Name2.tiff; % (transparent, obtained like Jonas
( <https://stackoverflow.com/questions/13660108/matlab-how-to-save-tiff-with-transparency-or-png-with-no-compression> ) but change "ch" with "K"
#
# Then:
# testbmp=imread('Name1.bmp');
# testtiff=imread(Name2.tiff');
# testbmp(:,:,4)=testtiff(:,:,4);
# imwrite(testbmp,'transparent_fusion.tiff');
  댓글 수: 1
Image Analyst
Image Analyst 2014년 9월 7일
Horia, we have no idea what this is. Please start a new discussion on it and paste the link back here.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by