morphology operation that generate an additional compact border region around shapes

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

morphology operation that generate an additional compact border region around shapes

Post by gubach »

I have a gray scale mask (https://www.flickr.com/photos/gbachelie ... ed-public/) that should be used in CopyOpacity to mask out regions with strong aliased edges (https://www.flickr.com/photos/gbachelie ... 7436918822).

My idea was to enlarge the black shapes of the mask (and add a blur to get a soft transition). I thought something like "-negate -morphology Thicken Disk:n -negate" with n=5, 10, ... would work, but aliased edges were still visible after a compose Over. A compare with the enlarged mask without blur shows a disconnected pixel cloud instead of an additional compact border region around the areas (https://www.flickr.com/photos/gbachelie ... ed-public/). So I added a blur (-blur 4x3) (https://www.flickr.com/photos/gbachelie ... ed-public/). The compare (https://www.flickr.com/photos/gbachelie ... ed-public/) shows an expected compact border but in the result compositing aliased edges were still visible. Perhaps the blur transforms black pixels inside the shape boarder to gray pixels that become transparent but the larger the border the less this this effect would be ...

Perhaps the morphology operation is not the right one to generate an additional compact border region around shapes that will mask out aliased edges supported by the first compare. Any idea on this matter? Thank you!

best regards
Günter
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: morphology operation that generate an additional compact border region around shapes

Post by snibgo »

Pretty pictures. What are they?

Looking at the 3000x3000 versions, the first image initially looks black and white only: two black blobs on a white background. It seems to be aliased (ie with steps). But it has been saved with JPG, and some edges are perhaps deliberately gray. Many pixels close to the black/white boundary are neither black nor white, and this may have been caused by the JPEG compression.

Aside from that, the mask seems to follow the black shapes in the first colour image exactly, with no need to expand it.

Code: Select all

convert 32314622152_d013e4a656_o.jpg 32061262473_c77cf14c4e_o.jpg -compose CopyOpacity -composite x.png
This makes the black areas from the colour images transparent. Isn't that what you want? Some pixels will be not quite right, because of the problems I mentioned above. "-threshold 50%" on the mask might fix that.
gubach wrote:Perhaps the morphology operation is not the right one to generate an additional compact border region around shapes that will mask out aliased edges supported by the first compare.
Sorry, I don't understand that.

Perhaps you want a slightly larger area to become transparent, with feathered transparency so you don't have a sudden transition from fully transparent to fully opaque.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: morphology operation that generate an additional compact border region around shapes

Post by fmw42 »

What about something like this, that blurs the edge, but uses -level to only blur the outside of your black area. I am using your 600 px versions.


Unix syntax:

Code: Select all

convert 32782813936_e665f21d08_z.jpg \
\( 32032969714_95bfabaf09_z.jpg -blur 0x2 -level 50x100% \) \
-alpha off -compose copy_opacity -composite result.png

For Windows,

Code: Select all

convert 32782813936_e665f21d08_z.jpg ^
( 32032969714_95bfabaf09_z.jpg -blur 0x2 -level 50x100% ) ^
-alpha off -compose copy_opacity -composite result.png
Adjust the blur size as desired and for your image resolution.

Please always provide your IM version and platform, since syntax differs.
gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

Re: morphology operation that generate an additional compact border region around shapes

Post by gubach »

@ snibgo: the pictures were generated by conformal maps (a sub set of complex functions) for image distortion programmed in Matlab. IMs fx can not handle complex numbers so I had to look for an other environment. But now I have the problem that I can not duplicate the CopyOpacity+Over pipeline from IM/PerlMagick in Matlab with an adequate quality see discussion in http://stackoverflow.com/questions/4213 ... scale-mask.
The problem is first the black areas in the RGB images; they must be masked out with a gray scale mask that is generated with the same conformal map therefore the position + shape corresponds. The second problem is that the black areas in both cases have aliased edges, therefore I want to optimize the mask (morphologically enlarge the black shapes + blur for soft transition) to eliminate the edge regions in the RGBs. See an image from https://www.flickr.com/photos/gbachelie ... 9863538505 for the aliased edge region in comparison to an image from https://www.flickr.com/photos/gbachelie ... 9935822485 that was made with a post processing PerlMagick script which serves a reference.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: morphology operation that generate an additional compact border region around shapes

Post by fmw42 »

If you are interested, I have a unix shell script that used IM -fx to compute a half dozen different conformal maps. I had to do the complex math myself and then separate the real and imaginary parts. See my script, conformal, at my link below. Also above, I show one simple method to antialias the mask.

What complex functions are you using?
gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

Re: morphology operation that generate an additional compact border region around shapes

Post by gubach »

@fmw42: I am in the experimental and development phase; I have used for the published images the bipole CM
a = 2; W = (1./(4*Z.^a - 1));
extracted from https://karczmarczuk.users.greyc.fr/TEA ... pStill.jar
I will look at the script and your proposal with -level soon. Thank you very much!

regards
Günter
gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

Re: morphology operation that generate an additional compact border region around shapes

Post by gubach »

@fmw42: Have taken a look at the CM image examples: Very!!!! interesting because of
-v vp ... VP is the virtual-pixel method. Any virtual-pixel method is allowed. The default=tile.

It seems that a virtual-pixel method can avoid the empty black spaces that I am trying to fight with those post processing compose approaches above
(never thought of this even I know them in the context of Arc/Polar).

In ComplexMapStill.jar such black areas never were shown therefore I was unpleasantly surprised as this happens with Matlab.

This would solve all the problems but I suspect that I will have no luck to get something like "tile" in Matlab. Nevertheless I am very interested to read more about virtual-pixel methods and get my hands on some source code (in the case I will have some resources for a freelancer.com C/C++2Matlab coder ) .

I will inform David Tschumperlé, the chief developer of http://gmic.eu/, about this who is very interested in porting some CM functionality with the math interpreter of gmic (http://opensource.graphics/image-proces ... evaluator/; much faster approach than fx because of pre-compiling)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: morphology operation that generate an additional compact border region around shapes

Post by snibgo »

In my view, IM's "-fx" feature is useful for prototyping, and for processing of small "images" that are really data for cluts or whatever, eg 1000x1 pixels. But it is far too slow for manipulation of "real" images.

Without building (part of) a C compiler into IM, I don't know how it might be made faster.

Building and installing a program that calls IM to do processing takes around 0.5 seconds. So, for people with compilers, a simple script could be built that took an arbitrary expression and plugged that into the source code of a C program, then built and ran it.

And that script could be written as a process module, so we could call it from convert or magick:

Code: Select all

magick in.png -process 'conform "W = (1./(4*Z.^2 - 1))"' out.png
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: morphology operation that generate an additional compact border region around shapes

Post by fmw42 »

Yes, I agree with snibgo. The script could be converted to a Magick Filter. But I was holding off until Magick Filters are better integrated with IM.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: morphology operation that generate an additional compact border region around shapes

Post by snibgo »

It could even be implemented as a built-in IM command:

Code: Select all

magick in.png -conform "W = (1./(4*Z.^2 - 1))" out.png
... but then a C compiler would be a pre-requisite for IM, which might be a step too far.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: morphology operation that generate an additional compact border region around shapes

Post by fmw42 »

Any given complex expression could be built into an IM function. You would have to choose from some numbered option for the given expression. The IM function would have to have a switch option for each possible function that it contains. Providing an arbitrary expression is much harder and why IM -fx is so slow (without a compiler).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: morphology operation that generate an additional compact border region around shapes

Post by fmw42 »

@gubach

I have added your expression 1/(4*z^2-1) to my conformal script. Let me know if it does not look correct.

It would be nice to see one of your input images.
gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

Re: morphology operation that generate an additional compact border region around shapes

Post by gubach »

About the management of complex functions: In Matlab each complex function has its own m-file like conformalInverse_0001.h:

function U = conformalInverse(X, ~)
%#codegen
U = [zeros(size(X))];
Z = complex(X(:,1),X(:,2));

% insert CM formula here
a = 2; W = (1./(4 * Z.^a - 1)); % bipole 1/(4*z^2-1)

U(:,2) = imag(W);
U(:,1) = real(W);

The m-file-name is then referred in a main program in maketform that is used in imtransform so a RGB image C is distorted into T with:

conformal = maketform('custom', 2, 2, [], @conformalInverse_0001, []);
T = imtransform(C, conformal, 'bicubic', 'UData', uData,'VData', vData, 'XData', xData,'YData', yData, 'Size', [cm_out_h cm_out_w], 'FillValues', FillValue );

with some parameters:
uData = [ -1.25 1.25]; % Bounds for REAL(w)
vData = [ 0.75 -0.75]; % Bounds for IMAG(w)
xData = [ -2.4 2.4 ]; % Bounds for REAL(z)
yData = [ 2.0 -2.0 ]; % Bounds for IMAG(z)
cm_out_h = 3000; % h of output image
cm_out_w = 3000; % w of output image
FillValue = 0; % color definition for the filling of empty areas; 0 == black

'FillValues', FillValue would be the starting-point for the virtual-pixel methods like tile.

Perhaps a folder with compiled functions for IM that are called by file name would be a flexible and fast way.


@fmw42 : > I have added your expression 1/(4*z^2-1) to my conformal script.
I have prepared (but not yet tested) all the expressions found in https://karczmarczuk.users.greyc.fr/TEA ... pStill.jar for usage in a Matlab context (see below). To decide what might be worthy I found a quick check with ComplexMapStill.jar a good method and it can be used for arbitrary! expressions.

% a = -2; W = (1./(Z.^a)); % 1/(z^a)

% a = 2; W = (1./(4*Z.^a - 1)); % bipole 1/(4*z^2-1)

% W = -5 .* ((Z.^3)./3 - Z./4)./2; %-5*(z^3/3-z/4)/2

% W = (2.*Z^4) + (4.*(1-1i).*(Z.^3)./3) - ((1+1i).*Z.^2) - Z; %2*z^4+4*(1-i)*z^3/3-(1+i)*z^2-z

% W = ((Z-0.5)./(Z+0.5)).^2; %((z-0.5)/(z+0.5))^2

% W = log(Z).*240./(pi.*320); %log(z)*240/(Pi*320)

%W = (log(Z).*(240./320+3.*1i))./(2.*pi); %log(z)*(240/320+3*i)/(2*Pi) imagenery unit 1i = i

%W = (log(Z-1./2)+log(Z+1./2)+log(Z-1i/2)+log(Z+1i./2)+log(Z))*(240./320-3.*1i)./(2.*pi); %(log(z-1/2)+log(z+1/2)+log(z-i/2)+log(z+i/2)+log(z))*(240/320-3*i)/(2*Pi)

%W = 1 + Z + (Z.^2)./2; % Taylor exp: 1+z+z^2/2

%W = ((Z-1)-(Z-1).^2/2+(Z-1).^3./3+(Z-1).^4./4-(Z-1).^5/.5+(Z-1).^6./6-(Z-1).^7./7).*240./(pi.*320); %Taylor log: ((z-1)-(z-1)^2/2+(z-1)^3/3+(z-1)^4/4-(z-1)^5/5+(z-1)^6/6-(z-1)^7/7)*240/(Pi*320)

%W = ((1+Z./6)/(1-Z./6)).^3; % Prod exp: ((1+z/6)/(1-z/6))^3

%W = cos(Z.*pi);

%W = sin(Z.*pi);

%W = (sin(Z.*pi))./(cos(Z.*pi)); %tan: (sin(z*Pi))/(cos(z*Pi))

%W = 1-(Z.*pi).^2./2+(Z.*pi).^4./24+(Z.*pi).^6./720-(Z.*pi).^8./40320; %Taylor cos: 1-(z*Pi)^2/2+(z*Pi)^4/24+(z*Pi)^6/720-(z*Pi)^8/40320

%W = (Z.*pi)-(Z.*pi).^3./6+(Z.*pi).^5./120-(Z.*pi).^7./5040; % Taylor sin: (z*Pi)-(z*Pi)^3/6+(z*Pi)^5/120-(z*Pi)^7/5040

%W = Z.*pi+(Z.*pi).^3./3+2.*(Z.*pi).^5./15+17.*(Z.*pi).^7./315; %Taylor tan: z*Pi+(z*Pi)^3/3+2*(z*Pi)^5/15+17*(z*Pi)^7/315
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: morphology operation that generate an additional compact border region around shapes

Post by fmw42 »

Too much effort for me to manually separate the real and imaginary components of all your expressions or even the more complicated ones. Sorry.
gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

Re: morphology operation that generate an additional compact border region around shapes

Post by gubach »

@fmw42: > Too much effort for me to manually separate the real and imaginary components of all your expressions or even the more complicated ones.
I had a look at your script and the transformation of a textbook expression to your syntax is indeed difficult.

I have made a first experiment to compare the script and Matlab.
I got some messages but it produced results

conformal.sh: Zeile 116: type: conformal.sh: Nicht gefunden.
dirname: fehlender Operand
„dirname --help“ gibt weitere Informationen.
basename: fehlender Operand
„basename --help“ gibt weitere Informationen

All images are 3000x3000:
input: https://www.flickr.com/photos/gbachelie ... ed-public/
script: https://www.flickr.com/photos/gbachelie ... ed-public/
Matlab: https://www.flickr.com/photos/gbachelie ... ed-public/

One can see that the script computes a more zoomed in part of the Matlab result. Perhaps you can give me a proposal for alternative values for "-g 1,1" to zoom out that both images are better comparable.

Processing time: I had stopped looking at 5 min (>300 sec) whereas Matlab took about 7 sec. I will try to plug it in my standard batch processing PerlMagick template (with a systemcall) to get a time statistic, but it is clear that IM-fx cannot be used this way in my art process with image populations of 1000 individuals.
Post Reply