- Home
- Search
- Contributors
- Recent Activity
- Flagged Content
- Log in to ask and answer questions.
- Ask a Question
- About MATLAB Answers

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Tomas
on 8 Nov 2012

Hello,

how can I decide that an image is symmetric or asymmetric in color after dividing it into two parts vertically (the right part and the left part)? thanks

Answer by Image Analyst
on 8 Nov 2012

You could chop the image in half, call fliplr() to flip one of the halves, then check similarity with SSIM (See wikipedia) or something similar like PSNR.

Show 16 older comments

Tomas
on 8 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_110255

I used fliplr() but i get this error

??? Error using ==> fliplr at 18 X must be a 2-D matrix.

Error in ==> code at 40 B = fliplr(A);

Image Analyst
on 8 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_110258

Then you have to extract each color channel

% Extract the individual red, green, and blue color channels. redChannel = rgbImage(:, :, 1); greenChannel = rgbImage(:, :, 2); blueChannel = rgbImage(:, :, 3);

and flip them one at a time, then recombine:

rgbImage = cat(3, redChannel, greenChannel, blueChannel);

Tomas
on 8 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_110268

I used this function of SSIM

function [mssim, ssim_map] = ssim_index(img1, img2, K, window, L) if (nargin < 2 || nargin > 5) ssim_index = -Inf; ssim_map = -Inf; return; end

if (size(img1) ~= size(img2)) ssim_index = -Inf; ssim_map = -Inf; return; end

[M N] = size(img1);

if (nargin == 2) if ((M < 11) || (N < 11)) ssim_index = -Inf; ssim_map = -Inf; return end %window = fspecial('gaussian', 11, 1.5); % % K(1) = 0.01; % default settings % K(2) = 0.03; % % L = 255; % end

if (nargin == 3) if ((M < 11) || (N < 11)) ssim_index = -Inf; ssim_map = -Inf; return end window = fspecial('gaussian', 11, 1.5); L = 255; if (length(K) == 2) if (K(1) < 0 || K(2) < 0) ssim_index = -Inf; ssim_map = -Inf; return; end else ssim_index = -Inf; ssim_map = -Inf; return; end end

if (nargin == 4) [H W] = size(window); if ((H*W) < 4 || (H > M) || (W > N)) ssim_index = -Inf; ssim_map = -Inf; return end L = 255; if (length(K) == 2) if (K(1) < 0 || K(2) < 0) ssim_index = -Inf; ssim_map = -Inf; return; end else ssim_index = -Inf; ssim_map = -Inf; return; end end

if (nargin == 5) [H W] = size(window); if ((H*W) < 4 || (H > M) || (W > N)) ssim_index = -Inf; ssim_map = -Inf; return end if (length(K) == 2) if (K(1) < 0 || K(2) < 0) ssim_index = -Inf; ssim_map = -Inf; return; end else ssim_index = -Inf; ssim_map = -Inf; return; end end

C1 = (K(1)*L)^2; C2 = (K(2)*L)^2; window = window/sum(sum(window)); img1 = double(img1); img2 = double(img2);

mu1 = filter2(window, img1, 'valid'); mu2 = filter2(window, img2, 'valid'); mu1_sq = mu1.*mu1; mu2_sq = mu2.*mu2; mu1_mu2 = mu1.*mu2; sigma1_sq = filter2(window, img1.*img1, 'valid') - mu1_sq; sigma2_sq = filter2(window, img2.*img2, 'valid') - mu2_sq; sigma12 = filter2(window, img1.*img2, 'valid') - mu1_mu2;

if (C1 > 0 && C2 > 0) ssim_map = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)); else numerator1 = 2*mu1_mu2 + C1; numerator2 = 2*sigma12 + C2; denominator1 = mu1_sq + mu2_sq + C1; denominator2 = sigma1_sq + sigma2_sq + C2; ssim_map = ones(size(mu1)); index = (denominator1.*denominator2 > 0); ssim_map(index) = (numerator1(index).*numerator2(index))./(denominator1(index).*denominator2(index)); index = (denominator1 ~= 0) & (denominator2 == 0); ssim_map(index) = numerator1(index)./denominator1(index); end %figure,imshow(ssim_map); mssim = mean2(ssim_map); %figure,imshow(mat2gray(ssim_map)) return

but i get this error

??? Undefined function or method 'conv2' for input arguments of type 'double' and attributes 'full 3d real'.

Error in ==> filter2 at 73 y = conv2(hcol, hrow, x, shape);

Error in ==> ssim_index at 136 mu1 = filter2(window, img1, 'valid');

Error in ==> ssim at 7 [mssim, ssim_map] = ssim_index(img1, img2, K, window, L)

Tomas
on 9 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_110275

if I have understood correctly, the 2 in the function name means CONV2 2D, so I can't apply this operation to a 3D matrix (resulting RGB image). So what is the solution??

Image Analyst
on 9 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_110285

Depends on how you want to define symmetry. Why don't you just convert to grayscale and check it? That should give a pretty good idea of whether it's symmetric or not.

Tomas
on 9 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_110385

I didn't understand what do you mean by 'how you want to define symmetry'. I just want to calculate the index of asymmetry. The index shows a result of evaluation of image's asymmetry It should be between 0 and 1. symmetric-->0 and asymmetric-->1. I think that converting the image to grayscale will not give the same result???

Image Analyst
on 13 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_110895

You could require it be symmetric for each of the red, green, and blue channels. Or you could require it be symmetric for each of the h, s, and v channels. Or you could define it to be symmetric for only the gray scale version, from rgb2gray(), which should give an image essentially the same as the v channel. Which one of those ways do you want to do it?

Tomas
on 13 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_111032

I want to find the score of asymmetry in color so I don't know what should I use rgb or hsv

Pamela
on 15 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_111410

Hello

function [mssim, ssim_map] = ssim_index(img1, img2, K, window, L) if (nargin < 2 || nargin > 5) ssim_index = -Inf; ssim_map = -Inf; return; end if (size(img1) ~= size(img2)) ssim_index = -Inf; ssim_map = -Inf; return; end [M N] = size(img1); if (nargin == 2) if ((M < 11) || (N < 11)) ssim_index = -Inf; ssim_map = -Inf; return end %window = fspecial('gaussian', 11, 1.5); % % K(1) = 0.01; % default settings % K(2) = 0.03; % % L = 255; % end if (nargin == 3) if ((M < 11) || (N < 11)) ssim_index = -Inf; ssim_map = -Inf; return end window = fspecial('gaussian', 11, 1.5); L = 255; if (length(K) == 2) if (K(1) < 0 || K(2) < 0) ssim_index = -Inf; ssim_map = -Inf; return; end else ssim_index = -Inf; ssim_map = -Inf; return; end end if (nargin == 4) [H W] = size(window); if ((H*W) < 4 || (H > M) || (W > N)) ssim_index = -Inf; ssim_map = -Inf; return end L = 255; if (length(K) == 2) if (K(1) < 0 || K(2) < 0) ssim_index = -Inf; ssim_map = -Inf; return; end else ssim_index = -Inf; ssim_map = -Inf; return; end end if (nargin == 5) [H W] = size(window); if ((H*W) < 4 || (H > M) || (W > N)) ssim_index = -Inf; ssim_map = -Inf; return end if (length(K) == 2) if (K(1) < 0 || K(2) < 0) ssim_index = -Inf; ssim_map = -Inf; return; end else ssim_index = -Inf; ssim_map = -Inf; return; end end C1 = (K(1)*L)^2; C2 = (K(2)*L)^2; window = window/sum(sum(window)); img1 = double(img1); img2 = double(img2); mu1 = filter2(window, img1, 'valid'); mu2 = filter2(window, img2, 'valid'); mu1_sq = mu1.*mu1; mu2_sq = mu2.*mu2; mu1_mu2 = mu1.*mu2; sigma1_sq = filter2(window, img1.*img1, 'valid') - mu1_sq; sigma2_sq = filter2(window, img2.*img2, 'valid') - mu2_sq; sigma12 = filter2(window, img1.*img2, 'valid') - mu1_mu2; if (C1 > 0 && C2 > 0) ssim_map = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)); else numerator1 = 2*mu1_mu2 + C1; numerator2 = 2*sigma12 + C2; denominator1 = mu1_sq + mu2_sq + C1; denominator2 = sigma1_sq + sigma2_sq + C2; ssim_map = ones(size(mu1)); index = (denominator1.*denominator2 > 0); ssim_map(index) = (numerator1(index).*numerator2(index))./(denominator1(index).*denominator2(index)); index = (denominator1 ~= 0) & (denominator2 == 0); ssim_map(index) = numerator1(index)./denominator1(index); end %figure,imshow(ssim_map); mssim = mean2(ssim_map); %figure,imshow(mat2gray(ssim_map)) return

This method can help me to measure the asymmetry index of a color region of interest?? knowing that the background is black. I have now two images, after dividing the segmented image, each containing a portion of the region although divided along the major axis. So how can I adapt this method to my need??

thanks

Pamela
on 19 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_111856

ssim can help me?? knowing that I have a color image. I need your help please

Image Analyst
on 19 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_111859

Where did you post examples of your symmetric and non-symmetric images?

Pamela
on 19 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_111956

I'm working on a large number of images with different colors. I want to calculate the score of asymmetry in color, texture and shape.

Examples of images after a segmentation step:

symmetric http://s11.postimage.org/5t2rws3/tachymmetric.jpg

asymmetric http://s9.postimage.org/xorjshb/tachsymmetric.jpg

Image Analyst
on 19 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_111964

It's a lot easier to just get the solidity. Will that work for you? For the two example you gave, solidity would work well for distinguishing between the two.

Pamela
on 19 Nov 2012

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_112080

I couldn't understand what do you mean by 'solidity'. But I will try to explain again: I want to take for example the first image and measure its score of asymmetry. This score should be between 0 and 1. I'm looking for a method to do this. So I'm asking for ssim if it can helps me.

Mariam Sheha
on 19 Jun 2013

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_155742

Hey Pamela,

I think you are working through melanoma diagnosis applying (ABCD rule), am working through the same target, where i am trying to calculate asymmetry score in color and texture for a segmented image... did you reach an effective method??!

Sidra Naeem
on 22 Nov 2014

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_251218

Hey I am also working on asymmetry of images Can anybody explain me the method how to implement this in Matlab?

Image Analyst
on 22 Nov 2014

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_251221

There is an ssim() function in the Image Processing Toolbox. Or you cuold look for published articles on it here: http://www.visionbib.com/bibliography/contents.html

Sidra Naeem
on 22 Nov 2014

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_251226

I haven't ssim() function in MATLAB 2013. Can you tell me some other method to implement this in Matlab

Image Analyst
on 22 Nov 2014

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/53215#comment_251233

**I did** , sort of - did you see the **link** I gave you? That will have the best methods by people who have studied it intensively. Maybe my *guess* of SSIM is not even the best way.

If you still want ssim(), upgrade to the latest version of MATLAB, or program it yourself with information from http://en.wikipedia.org/wiki/Structural_similarity

- © 1994-2015 The MathWorks, Inc.
- Site Help
- Patents
- Trademarks
- Privacy Policy
- Preventing Piracy
- Terms of Use
- RSS
- Google+

- Featured MathWorks.com Topics:
- New Products
- Support
- Documentation
- Training
- Webinars
- Newsletters
- MATLAB Trials
- Careers

## 0 Comments