Removing red eye from an Image on the client side using Jquery - php

I have the following html code rendered on my client's browser:
<div id="Div">
<img src="myImage.jpg" id="myImage"/>
</div>
This particular image is uploaded by the user and then displayed here.
I need to allow my user to remove any red-eye from this image. I would like to do it without any postback ( I'm using CodeIgniter at the back ). Are there any available libraries for this in JQuery (or plain Javascript) ? If not what could be a good approach ?

There is a lot of things that go on in red eye removal
A. Eye Detection
B. Red Eye Region Mapping
C. Fill Color
D. Fuzz
E. Opaque
My advice
If not a JOB for Jquery and even PHP would not remove red eye effectively
Likely Solution
Get a Jquery area selection script where users can select their red eyes them self ( With this you would be able to get the region (X1 , Y1 , X2 , Y2 , Height , Width ) example http://odyniec.net/projects/imgareaselect/
Have a simple Color Picker where they can select replacement color ??? Default can be black
Send request to imagemagick using exec in PHP for the red eye removal
You can not output your image ...
EDIT 1
I was able to help you get a ready command line tool for this JOB
http://www.fmwconcepts.com/imagemagick/index.php
http://www.fmwconcepts.com/imagemagick/redeye/index.php
Basic Concept
A. Create a desaturate copy of the input image
B. Perform a fuzzy floodfill to create a mask image
C. Composite the original with the desaturated image using the mask image
D. Apply a morphologic close operation to fill in the specular hole in the mask
and then create a difference operation to create a new mask of just the hole
E. Apply the new mask to composite the previous result with a full lightness,
zero saturation version of the original image
Sample Process
convert -quiet -regard-warnings "$infile" +repage "$tmpA1"
convert $tmpA1 -modulate $light,$sat,100 $tmpA2
proc=""
for ((i=0; i<np; i++)); do
proc="$proc matte ${pairArray[i]} floodfill"
done
convert $tmpA5 -fuzz $fuzz% -fill none -draw "$proc" \
-fill "rgba(255,255,255,1)" +opaque "rgba(0,0,0,0)" \
-fill "rgba(0,0,0,1)" -opaque "rgba(0,0,0,0)" \
-alpha off -negate $tmpA3
if [ "$dilate" = 0 ]; then
dilation=""
else
dilation="-morphology dilate disk:$dilate"
fi
convert $tmpA1 $tmpA2 $tmpA3 -compose over -composite $tmpA2
convert $tmpA3 \( +clone -morphology close disk:$rad $dilation \) \
-compose difference -composite -auto-level \
-negate -threshold 0 -negate $tmpA4
convert $tmpA2 \( $tmpA1 -modulate 100,0,100 \) $tmpA4 \
-compose over -composite $outfile
I hope this helps
Thanks
:)

Related

Mask PNG image with black and white mask

I have the following image (please note the transparent background):
I also have a black/white mask of the same size:
I would like to "crop" the dress and get just the portion of the first image contained in the black circle. I tried many different methods but they didn't work or are too slow:
1) ImageMagick (command line) <== which command can I use to achieve this? I tried multiply and copyopacity but they didn't work
2) WideImage is working: $maskedImage = $source->applyMask($mask); but it takes more than 12 seconds.
I am interested in a ImageMagick solution if possible.
EDIT
The provided solutions work fine if the mask is smaller than the original image and if the original image is simple. With these source image and mask the result is "smeared":
Source:
Mask:
Command:
convert source.png \( mask.png -negate \) -alpha off -compose copy_opacity -composite result.png
Result (I added a grey background instead of the transparent one in order to show the wrong white):
I think you want this:
magick dress.png \( mask.png -alpha off -negate \) -compose copyalpha -composite result.png
Or, if you dislike parentheses, load the mask and sort out your alpha channel first, then load the dress, then +swap the order before compositing:
magick mask.png -alpha off -negate dress.png +swap -compose copyalpha -composite result.png
Your version of ImageMagick appears to be too old to include the "copyalpha" compose operator. Here's another way to get your result...
convert dress.png \( circle.png -negate \) \
\( -clone 0 -transparent red +transparent red \) -insert 0 -composite result.png
That reads in your main image, then reads in your mask image and negates it, then creates a transparent layer and moves it to the first position in the list with "-insert". ImageMagick's default handling of "-composite" with three input images is to use the third image, now the one with the black circle, as an alpha mask. You still have to "-negate" that mask, or make a new mask with the black and white inverted.
The method used there to create the transparent canvas is to read in one of the other images inside parentheses, change everything red to transparent, then change everything not red to transparent. That results in an entirely transparent canvas to use as the first image, the destination image, in the composite list.
It should work fine in either ImageMagick 6 or ImageMagick 7 using copy_opacity not copy_alpha. This works fine for me:
Input:
Mask:
convert dress.png \( mask.png -negate \) -alpha off -compose copy_opacity -composite result.png
The above command using convert is for ImageMagick 6. If using ImageMagick 7, change convert to magick. Both work for me.
At the end of the day I kept using WideImage which is quite slow but works well. This is the class I use to mask images:
<?php
namespace AppBundle\Service\Import;
use WideImage\WideImage;
class ImageMasker
{
/**
* #var string
*/
private $tempDirectory;
public function __construct(string $tempDirectory)
{
$this->tempDirectory = $tempDirectory;
}
/**
* #param string $sourcePath
* #param string $maskPath
*/
public function mask($sourcePath, $maskPath)
{
$source = WideImage::load($sourcePath);
$mask = WideImage::load($maskPath);
$tempFilename = uniqid().'.png';
$tempPath = rtrim($this->tempDirectory, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$tempFilename;
// applies the mask and saves the file
$maskedImage = $source->applyMask($mask);
$maskedImage->saveToFile($tempPath);
return $tempPath;
}
}

Extracting certain parts on image with PHP

I would like to be able to extract radar data from gif which occurs every hour.
This is an example of one gif:
Data colors can vary according data legend on the right. I would also like that country borders are not visible, as gif is drown in order: base layer, radar data layer, borders layer - I only need pure radar data.
Any suggestion or solution will be very much appriciated!
All the radar data seems to be highly saturated, so you could extract the saturation onto its own layer and threshold it at, say, 85% and use that as the opacity like this:
convert radar.gif \
\( +clone -colorspace hsl -channel S -separate -threshold 85% \) \
-compose copyopacity -composite result.gif
which gives you this, where all the unsaturated colours are now transparent:
As you can see, it is correctly picking up and retaining all the colours in the "key" on the right, but it also picks up the mauve on the left of the image. Let's make that specific tone transparent:
convert radar.gif \
\( +clone -colorspace hsl -channel S -separate -threshold 85% \) \
-compose copyopacity -composite \
-fuzz 10% -fill none -opaque "rgb(160,153,255)" result.gif
which gives this:
Now, I presume we need to get rid of the sickly yellow too:
convert radar.gif \
\( +clone -colorspace hsl -channel S -separate -threshold 85% \) \
-compose copyopacity -composite \
-fuzz 10% -fill none \
-opaque "rgb(160,153,255)" \
-opaque "rgb(254,251,175)" result.gif
Finally, let's crop to the top left 480x480 px:
convert radar.gif -crop 480x480+0+0 +repage \
\( +clone -colorspace hsl -channel S -separate -threshold 85% \) \
-compose copyopacity -composite \
-fuzz 10% -fill none \
-opaque "rgb(160,153,255)" \
-opaque "rgb(254,251,175)" result.gif
You can use 'imagecreatefromgif' to retrieve the gif and load it as an GD object (requires GD enabled in the php.ini). From there you can use quite a few image manipulation tricks to cut down to what you want from the gif. The PHP.net site has an example on what you can do with it.
First of all, if you are interested just in the information coded by colours, which us present in some specific frame of the gif, read here about how you can access only one frame if that gif:
Accessing gif frames
After that, considering you have a rectangle image, you can scan pixel by pixel, as a matrix, using php function 'imagecolorat'.

Imagemagick clipping out shapes and adding an expanded background

Using imagemagick, I'm looking to add an expanded background color to an image. My source looks like this:
And this is the desired output (the background color will be white, but this is for clarity):
Step 1 seems to be filling the background with my desired color, but I can't find a way to remove the background outside the shape. This will also be a problem if the source image contains any of the desired background color already (because it will be made transparent).
Ideas?
It is not perfect but should do the job:
convert ( in.png -resize 200% -flatten -negate -morphology Dilate Disk:20 \
-fuzz 90% -fill none -draw "matte 0,0 floodfill" -fill green \
-colorize 100% -resize 50% ) in.png -composite out.png

How to make fade edges in image magic?

I want to create faded edges image effect using imagemagic commands. Please help me for a command to make effect as like http://postimg.org/image/h51e4twyp/
Start with Doge
curl -o doge.jpg http://i0.kym-cdn.com/photos/images/newsfeed/000/581/296/c09.jpg
Next, blur the edges.
convert doge.jpg -alpha set -virtual-pixel transparent -channel A -morphology Distance Euclidean:1,20\! +channel doge.png
You will need to output it as a .png
Imagemagick documentation
Edit: The above example is for ImageMagick v6.
For IMv7 try:
convert doge.jpg \( +clone -alpha extract -virtual-pixel black -gamma 2 +level 0,100 -white-threshold 99 -morphology Distance Euclidean:1,200! -sigmoidal-contrast 3,0% \) -compose CopyOpacity -composite doge_im7.png

Crop image on angle

I'm currently looking for a way to crop an image, but on an angle.
I don't think I can just rotate the image first as the script is supplied with specific x,y coordinates of each corner.
So if you can imaging this, image is uploaded, 1280x720.
Along with the image it's supplied with x,x coordinates for the crop zone.
However the top left and top right coordinates will not have the same y position.
Heres an examples
Before
After
Any ideas ?
You'll still need to use trigonometry methods to rotate the image, but you can mimic a crop-at-an-angle by mixing opacity copying and trimming.
First. Create an Image Mask
If all the points are giving to you, and the image size is defined, simply draw the area that needs to be extract
WIDTH=819
HEIGHT=616
TOP_LEFT=669,117
TOP_RIGHT=784,155
BOTTOM_LEFT=544,495
BOTTOM_RIGHT=659,534
convert -size $WIDTHx$HEIGHT xc:black -fill white -stroke white \
-draw "polyline $TOP_LEFT $TOP_RIGHT $BOTTOM_RIGHT $BOTTOM_LEFT" \
mask.png
Masking and Background Removal
This method of masking will turn off the alpha-channel and set the background to transparent. When we compose the two images, the resulting image will only display what's within the area we defined in the mask. (note: you may need to adjust the -background to white, or transparent.)
convert source.jpg mask.png -alpha Off -compose CopyOpacity \
-composite -background transparent copyOpacity.png
Calculate Degree to Rotate
If you have two points on a square angle, you should be able to follow the atan method. Most language will have an atan2 function. Other trigonometry questions "Rotating a rectangle" & "How to calculate the angle between two points relative to the horizontal axis?"
DELTA_Y=$(($HEIGHT-155-534))
DELTA_X=$((784-659))
DEGREE=`awk "BEGIN { pi=4.0*atan2(1.0,1.0)+90; print atan2($DELTA_Y,$DELTA_X)*180/pi; }"`
convert copyOpacity.png -rotate $DEGREE -trim final.png
Luckily, you can do everything in one step.
#!/bin/bash
WIDTH=819
HEIGHT=616
TOP_LEFT=669,117
TOP_RIGHT=784,155
BOTTOM_LEFT=544,495
BOTTOM_RIGHT=659,534
DELTA_Y=$(($HEIGHT-155-534))
DELTA_X=$((784-659))
DEGREE=`awk "BEGIN { pi=4.0*atan2(1.0,1.0)+90; print atan2($DELTA_Y,$DELTA_X)*180/pi; }"`
convert source.jpg \( -size $WIDTHx$HEIGHT xc:black -fill white -stroke white \
-draw "polyline $TOP_LEFT $TOP_RIGHT $BOTTOM_RIGHT $BOTTOM_LEFT" \) \
-alpha Off -compose CopyOpacity -composite \
-background transparent -rotate $DEGREE -trim \
final.png

Categories