I have a massive set of noisy images of drawings that people have created. I'd like to have some function to trim them down to ONLY the drawing.
Here are some examples:
Because of the noise -trim doesn't work
I also tried to use the example linked here (www.imagemagick.org/Usage/crop/#trim_blur), but it was ineffective because of differing noise levels both within and between images.
Lastly, I tried to increase the contrast to increase the likelihood of the lines of the actual drawing being identified, but for similar reasons to the above (differing noise levels), it only sharpened the lines in part of each image.
If anyone has any ideas, I'd love to hear them!
Not sure if this will work for all your images, as there are quite a few problems with them:
artefacts around the edges
uneven lighting, or shadows
noise
low-contrast
but you should get some ideas for addressing some of the issues.
To get rid of the artefacts around the edge, you could reduce the extent of the image by 2.5% on all sides - essentially a centred crop, like this:
convert noisy1.jpg -gravity center -extent 95x95% trimmed.png
To see the shadows/uneven lighting, I will normalise your image to a range of solid black to solid white and you will see the shadow at bottom left:
convert noisy1.jpg -normalize result.png
To remove this, I would clone your image and calculate the low frequency average over a larger area and then subtract that so that slowly changing things are removed:
convert noisy1.jpg \( +clone -statistic mean 25x25 \) -compose difference -composite -negate result.png
That gives this, and then you can try normalising it yourself to see that the shadow is gone:
If I now apply a Canny Edge Detection to that, I get this:
convert noisy1.jpg \( +clone -statistic mean 25x25 \) -compose difference -composite -normalize -negate -canny 0x1+10%+30% result.png
Here is a very crude, but hopefully effective, little script to do the whole lot. It doesn't do any checking of parameters. Save as $HOME/cropper.
#!/bin/bash
src=$1
dst=cropped-$1
tmp="tmp-$$.mpc"
trimbox=$(convert "$1" -extent 95x95% -write "$tmp" \( +clone -statistic mean 25x25 \) -compose difference -composite -normalize -negate -canny 0x1+10%+30% -format %# info:)
convert "$tmp" -crop $trimbox "$dst"
rm tmp-$$.*
Make the script executable with:
chmod +x $HOME/cropper
And run with a single image like this:
cd /path/to/some/images
$HOME/cropper OneImage.jpg
If you have hundreds of images, I would make a backup first, and then do them all in parallel with GNU Parallel
parallel $HOME/cropper {} ::: *.jpg
Related
I have used the following imagemagick command for the image below:
convert img.png -define morphology:compose=darken -morphology Thinning Rectangle:17x1+0+0\< tmp.png
This removes ALL lines from the image, but I just want to remove the small horizontal and vertical lines on the right and bottom of the number in top left corner of each block. I want to preserve the main column and row lines. Can anyone tell me how to do it?
This is what I get (notice the long lines dividing the image content into columns and rows are also gone. I want those line to stay):
I notice the script is finer/thinner and less regular than the lines so it is more susceptible to eroding techniques. With that in mind we can ditch the text like this:
convert vcards.png -colorspace gray -threshold 50% -morphology erode disk:1.5 +repage z1.png
That's a good start, but if we use that as a mask, we will lose the long horizontal lines in your original image. So, we can find all those by projecting all the rows into a single-pixel wide tall column and thresholding all the rows that are more than 80% white. Then widen the image back out to its original width.
convert z1.png -colorspace gray -resize 1x\! +repage -threshold 80% -scale 810x1518\! +repage z2.png
Now combine the two masks so they only do the lower and right sides of your little title box things.
convert z1.png \( z2.png -negate \) -compose darken -composite z3.png
Finally, fatten that mask up a bit because it may have shifted around during previous processing, and apply it to your original image.
convert vcards.png \( z3.png -morphology dilate disk:2 -negate \) -compose darken -composite result.png
It could all be combined into a single command, but I won't do that, because some aspects may not work for all your images and while they are all individually implemented and documented, they are simpler to improve or correct individually.
you can use the followin code for horizontal an change it slightly for vertical lines
magick 21.gif -monochrome ( +clone -negate -statistic median 219x1 ) -compose lighten -composite q1.png
I have several pictures of a landscape.
Using the ImageMagick CLI on OSX, I would like to distort and overlay them properly.
I have looked for distortion coordinates between several of the pictures and a reference picture. I fail to understand the diference between -distort and +distort and how it plays with +repage. When I use -distort, the output has the desired offset but it's incomplete (it needs to be bigger). When I use +distort, I get the full image but it's missing the offset.
Reading the documentation I understand that I could do without the offset if I did the overlay composition in the same command before the offset information is lost but what's happening is that the distort is being applied to both the reference and the distorted images.
This is the result of using -distort:
This is the result of using +distort:
The offset of the -distort result would work once I apply it as an overlay (here using the composite in a separate command, but it's missing a big chunk of the picture.
When I tried to consolidate it in a single command this is the result I get:
This is the command I'm currently using:
convert base.jpg overlay.jpg
-matte -virtual-pixel transparent -distort Perspective '961,1695 1856,2461 2279,1520 3185,2303 3564,2173 4441,2970 1547,2817 2441,3594'
-compose blend -define compose:args=50,100 -composite result.jpg
I understand I could use parenthesis there but I fail to see where should I put them.
Thanks!
Update: this is the result of the overlay when using +distort either in two steps or in a single step as recommended by Mark.
The solution was to use -flatten instead of -composite.
convert base.jpg \( b.jpg -matte -virtual-pixel transparent +distort Perspective '961,1695 1856,2461 2279,1520 3185,2303 3564,2173 4441,2970 1547,2817 2441,3594' \) -compose blend -define compose:args=100,50 -flatten result.jpg
Turns out that -composite ignores the image offsets whereas -flatten works with layers and uses the offset information.
The suggestion came from this thread: http://www.imagemagick.org/discourse-server/viewtopic.php?t=20157
This is the documentation to flatten (link broken in the discussion above): http://www.imagemagick.org/Usage/layers/#flatten
Not sure I understand the issues, but would suggest you try this (untested):
convert base.jpg \
\( overlay.jpg -matte -virtual-pixel transparent -distort Perspective '961,1695 1856,2461 2279,1520 3185,2303 3564,2173 4441,2970 1547,2817 2441,3594' \) \
-define compose:args=50,100 -compose blend -composite result.jpg
That would mean that the perspective distortion is only applied to the overlay, not the base. So, in the code above, the first line only processes the base image, the second line only processes the overlay, and the final line blends the two.
I've got one background image: sky.jpg
And two transparent PNG images: gradient.png and tree.png
Now I want to draw the two images on the background with a perspective distortion, like this:
The destination coordinates of the two images are, in clockwise order (starting top left) :
gradient: 62,129 421,218 383,458 147,548
tree: 445,100 765,47 698,368 529,396
I cannot figure out how to start with one image (in this case the sky background) and then take another image and draw that with perspective distortion to specific destination coords within the background. Also doing this with more than one image at a time, within one convert command, troubles me.
For example, when I start with just one image (the gradient) and try this:
convert sky.jpg \( gradient.png -alpha set -virtual-pixel transparent \
+distort Perspective "0,0 62,129 255,0 421,218 255,255 383,458 0,255 147,548" \) \
-compose src-over -composite result.jpg
It gets correctly warped (so the coordinates are relatively correct) but it's drawn in the top left corner, not at the coordinates I specify.
Also I'm a bit unsure if my usage of -compose and -composite is correct (I took this from various IM manual examples).
One other thing that is unclear to me: in case of the 256x256 image, should I use 255,0 and 255,255 and 0,255 as the corner coordinates, or 256,0 and 256,256 and 0,256 ?
Any IM experts who can shed light on these issues?
Add a -geometry just before the -composite like this:
convert -size 800x600 xc:black \( -size 300x200 xc:red \) -geometry +90+10 -composite result.png
Here's the original image I'm trying to remove background from:
I am trying to use imagemagick to remove the background from an image. When the image has a white product, my script doesn't work well. It removes the white from inside the product also. In brief I'm trying to do the following
create a mask image (replace background pixels with white with fuzz and threshold)
apply the mask over the original image to generate the output
If I use a fuzz factor of 0, like shown below, i get the background removed, but it creates a nasty halo around it. What can be done here?
I would take advantage of HSL colorspace, and create an alpha mask from the lightness channel.
convert tshirt.jpg \( \
+clone -colorspace HSL -separate \
-delete 0,1 -fx 'u>0.975?0:1' \) \
-compose CopyOpacity -composite \
out.png
I would go for a simple threshold to pick out the white and then some sort of filtration to remove the noise/ragged edges. So, for example
convert shirt.jpg -threshold 99.99% -negate result.jpg
which gives this:
Then apply some median filtering to smooth it:
convert shirt.jpg -threshold 99.99% -median 5 -negate result.jpg
or maybe a bigger filter:
convert shirt.jpg -threshold 99.99% -median 11 -negate result.jpg
which gives this
Alternatively, you may get on better with an erosion and a dilation...
convert shirt.jpg -threshold 99.99% -negate \
-morphology erode diamond:3 \
-morphology dilate diamond:3 result.jpg
You may like to use Anthony Thyssen's flicker_compare to flicker between the input and result image to see what you have got, see here.
./flickercompare -o flick.gif shirt.jpg result.jpg
We have the following PSD
https://app.box.com/s/rf514j3wnic1xkt6y1q3b5qnk0zds2rl
This is a transparent PSD with one base layer for the ring metal.
And additional layer for each individual stones.
Something like https://app.box.com/s/i8lhshbl27pvjmmczjq2bhla4mzw9cwn
and this
We want to be able to apply tint to each individual stone layer based of user input and export the final image as PNG file. To provide users the ability to design the ring to their liking.
Now I am trying to figure out what is the best way to achieve this functionality ?
We would like the image generated server side in .net so it can be shared.
What is the best way to approach this. I am thinking of using an image library like imagemagick to convert the image.
But I am unable to locate any examples where you can alter multiple layer with in a PSD file before converting it to another file format.
Any example or suggestion on methods to achieve this will of great help.
Editing layered PSD files is not going to be easy. I would suggest you maybe save the ring and each of the gemstone layers as a separate PNG file. Then you can do something along these lines with ImageMagick:
#!/bin/bash
convert ring.png \
\( layer-1.png \( +clone +level-colors red \) -compose Multiply -composite \) -compose overlay -composite \
\( layer-2.png \( +clone +level-colors green \) -compose VividLight -composite \) -compose overlay -composite \
\( layer-3.png \( +clone +level-colors blue \) -compose LinearBurn -composite \) -compose overlay -composite \
\( layer-4.png \( +clone +level-colors "#ffff00" \) -compose Saturate -composite \) -compose overlay -composite \
result.png
Giving you a PNG file like this:
You may like to experiment with the blending modes, I just tried a few that looked vaguely pleasing to my eye. If you want a list of all available blending modes, you can do:
identify -list compose
Atop
Blend
Blur
Bumpmap
ChangeMask
Clear
ColorBurn
ColorDodge
Colorize
CopyBlack
CopyBlue
CopyCyan
CopyGreen
Copy
CopyMagenta
CopyOpacity
CopyRed
CopyYellow
Darken
DarkenIntensity
DivideDst
DivideSrc
Dst
Difference
Displace
Dissolve
Distort
DstAtop
DstIn
DstOut
DstOver
Exclusion
HardLight
HardMix
Hue
In
Lighten
LightenIntensity
LinearBurn
LinearDodge
LinearLight
Luminize
Mathematics
MinusDst
MinusSrc
Modulate
ModulusAdd
ModulusSubtract
Multiply
None
Out
Overlay
Over
PegtopLight
PinLight
Plus
Replace
Saturate
Screen
SoftLight
Src
SrcAtop
SrcIn
SrcOut
SrcOver
VividLight
Xor
I also specified some colours by name and one by hex, so you can see how to do it that way if you prefer.
P.S. If you keep all your artwork as PSD files, you can always use Adobe's ExtendScript to script the export of the various layers as separate PNG files with a single keypress...
P.P.S. It is possible for ImageMagick to extract the layers from a PSD file itself, but I extracted the layers from your file and they are not all the same size as the background image and I cannot find their correct positioning relative to it. If you know something about how the layers were created and whether they could be made identical size and aligned with the background, you could replace layer-n.png in my examples with PhotoshopFile.psd[n]
P.P.P.S. If you want to do this server side in .NET you should take a look at the .NET library for ImageMagick that can be found here: https://magick.codeplex.com/. If you need help translating the commands above to C# you could ask a question on the discussions page there.