Finding blank areas of an image file using ImageMagick - php

I need to be able to determine if a part of an image (outside of predetermined crop marks) contains any image content.
Is there a way with ImageMagick (specifically the php interface) to do this?
Scenario:
The canvas is 8.5x11 with .5 in margins on the top, left, and bottom edges and a 1 in margin on the right edge. The image needs to fit within the crop marks for printing.
Normally I use Photoshop actions to do this, but I am trying to automate the process.

Replace everything within crop marks with black/white rectangle, do a histogram of the resulting image and analyze it?
I can do this with command-line version of ImageMagick, but dont know how to express it with PHP api.

Related

Fastest way to extract a crop from a JPEG file on demand?

Users on my website can click a portion of a photo to get a closeup of it.
I currently have a bit of JavaScript that fires off an AJAX call to a PHP script that uses ImageMagick to retrieve the relevant crop.
Could I be doing this better, outside of PHP? Using ImageMagick directly somehow, or something else?
Currently the files reside on the same server as the main website, but due to space restraints I'm in the process of moving them to a separate server, so will need to make a call between the two somehow.
The photos vary in size, some are 1600x1200 and only 200KB, others are 24MP+ 20MB+ originals.
Using ImageMagick, you have two options:
Crop the image while loading it. What is then loaded initially is a cropped image.
Load the image, then crop it. What is initially loaded is the complete image.
The first method is more efficient and faster.
This method is to append the image area information to the input image(s) in square brackets ([...]) like this:
convert wizard:[130x150+80+80] -resize 200% wiz-head.png
This will crop a piece of 130x150 pixels with an offset of 80 pixels from the top left corner of the original, built-in wizard: image. Here are both images side by side, wizard: (left) and the cropped section, resized by 200%:
If you wanted to crop a JPEG, you'd use something like:
convert some.jpeg[330x250+180+280] -resize 300% output.png

Replacing detected object in a frame with an image.(imageProcessing)

Overview:
I am working on a video creation project. The technology I am using are: imageMagick, php, ffmpeg.
Current Status:
Currently the project is able to create videos using images and texts and few basic transitions. The way I am doing it is using imagemagick to create gif using input images(with transition effects in them) and then converting all gifs to videos and atlast concatenating the video together.
Next Move (My question):
I am now set to take it to the next level. So, what I am having is a video(1920x1080) with some white frames(1280x720) that keeps shifting in each frame. I want to replace those white frames appearing in some frames of the video with some images(1280x720) that I wish to use. Please see the image here and you will get an idea: These are just two frames from my video. If you can see carefully the images are shifting(white space is not constant).
Expectation:
So, I want to fill those white space with one of my own image. If the case would have been for only one frame I could have used ffmpeg to overlay image on the exact width and height. But here the white space is not fixed and keeps shifting in all the frames and there are a lot of frames. So, I am looking for something like opencv or some other technology that can be used for object detection in a video or in a set of frames and replace the detected area with some other image.
I just need a kick. So, if anyone has already worked on something like this just suggest me what technology can I use. Thanks in advance.
It all depends on exactly what you can assume :
If you can safely assume that your rectangle's boundary is never occluded (hidden) somehow, you can try finding the edges in your image (like OpenCV's Canny edge) and then look for rectangular shape (corners forming a warped rectangle, or the very popular Hough Lines).
If the rectangle you're looking for is always white, you can threshold the image in a colorspace like HSV to look for maximum value (the V in HSV ~ brightness) then rectangular shape search in a binary image.
If your corners are occluded sometimes you'll have to do some tweaking with your image, like morphological operations ("grow and contract" binary thresholded image), then Hough Lines could do the trick.
Note that this answer assumes that once you know where the rectangle is, "you're done", and you just have to overwrite the rectangle with custom content.
I also do not check for any time-continuity : you video frame might jump around based on the frame-by-frame appearance of rectangle. You'd have to include some knowledge about previous positions.

How to I crop an image based on a bounding box in the image (programatically)?

I'm building a web application that needs to be able to crop an image based on the location of a 4-sided shape with a white border (and no fill) contained in the image. All images uploaded by users are going to be dark so they shouldn't interfere with the algorithm to find the bounding box. This part of the application should be able to handle all types of quadrilaterals and will stretch whatever is contained within the white-bordered shape to fit a square.
My lazy rendition in Photoshop: http://i.stack.imgur.com/xJjoL.png
I'm using Laravel to build this application (LAMP stack). I'm looking at libraries such as ImageMagick and GD but I'm still new to programatically editing images. Are there specific functions in IM or GD that I could call to help me build this part of the application? Are there better PHP libraries that have the ability to do this? I'm not asking for code necessarily; I just want to know what tools I should start with before I build this.
Imagemagick can be used for this type of application if I understand what you're asking. To change the perspective of an image, look into:
http://www.imagemagick.org/Usage/distorts/#perspective
To crop images after distorting, look into:
http://www.imagemagick.org/Usage/crop/#crop
It would be easy to make a bash loop to accomplish these two tasks. Something like this:
## first distort the images
for i in *.jpg ##or whatever format you have (.tiff, pdf, etc..)
do
convert $i -distort Perspective out_file.png ## where you give coordinates into the perspective method
done
## now to crop the distorted images
for i in *.png
do
convert $i -crop out_file.jpg ## where you give the coordinates of the new rectangle to the crop method
done
The code above is just a STARTING point and would need to be populated according to the links I mentioned above. Happy scripting!
You may want to look into OpenCV for recognizing the rectagle. GD and image magic are good at editing images using filters, but they don't do a lot (or any?) of object detection.
See: https://github.com/ProGM/OpenCV-for-PHP/wiki/Installation-guide

PHP - Crop an image, and center the resulting image onto a background, using GD

I'm trying to make a simple app which gives a user a standard background/template image onto which they can place their logo/brand/whatever. I've had a look at a few jQuery plugins which allows the user to upload and crop an image, and most of them seem to work by posting the x and y coords, and the width and height, to the server.
I'm able to then use imagecopyresampled() to merge the image with the background image, but (a) the user's image seems to end up really skewed, even when I hard-code dimensions that I know should work, and (b) I need to position the uploaded image pretty much dead-center -- what's the best way to achieve this?
Any help? This is my first time using GD. I'll add my own code if required, but I'm messing with the different GD functions, so I've butchered the one I had before. Will reproduce it.
Thanks

How would I programmatically crop an image to a non-rectangular shape?

I want to crop a rectangular image to a non-rectangular shape. I realize that if you take that completely literally, it's not possible. What I want to end up with is image X, cropped to shape Y, on a transparent background.
Let's say for example that I want to take a picture of the Idaho flag and crop it to the shape of the state of Idaho. I imagine I would do something like this:
Create an image that has opaque pixels for the shape of Idaho, transparent pixels everywhere else
Read and store some kind of bitmap for this Idaho state image
For each opaque pixel location in the Idaho state image, copy the corresponding pixel from the Idaho state flag image and place it on a blank, transparent canvas
Step 1 would obviously be done manually, but the rest would be done programatically. I think I have the right idea in general but I don't know how I'd approach the specifics. Can anyone point me in the right direction?
As far as implementation technology goes, I'm a PHP guy, so using gdLibrary or something that works with PHP would probably be the best way for me to go.
I would think it would be much easier to do by simply adding a bitmask or alpha channel. In that case you would use a negative mask image of your shape and then simply apply it to he regular image as a mask and then save out in a transparent format. Ive never actually done this with GD or ImageMagick but i would think its available as Jerry suggests.
Acutally here is a blog post form a similar SO question that might help: http://about.phalacee.com/geek/creating-mask-layers-using-php-gd
The usual way to do something like this would be to use white (all 1's) and black (all 0's) for your outline instead of transparent and opaque. Then you AND that image with the image you're trying to crop. The result is 0's where the outline image had 0's, and the other image where the outline had 1's.
You may also need to invert your outline image, and AND the inverted version with the background image. Then you OR the background image with the foreground image to produce a composite of the foreground image in the shape of Idaho (to use your example) and the background image everywhere else.
Depending on the capabilities of the library you're using, there's a good chance that kind of capability is directly available though. Just for example, Windows has this capability in the MaskBlt function.
In php using ImageMagick is probably your best bet:
$source = IMagick("sourcefile");
$mask = IMagick("maskfile");
$mask->adaptiveResizeImage($source->getImageWidth(), $source->getImageHeight(), true);
$source->compositeImage($mask, imagick::COMPOSITE_MULTIPLY, 0, 0);
$source->writeImage("newfile");
$source->clear();
$source->destroy();
sourcefile must be the source image as you like, maskfile must be a mask file that has the alpha channel set correctly for the shape you want.

Categories