Increase image size by power of two without smoothing - c++

In C++, or through a Linux shell script, how can I up-size an image by powers of 2 (e.g. a 40x60 image to 80x120) without any interpolation, so it comes out with a crisp pixelated looks like in the following image. Whenever I do it using Magick++'s Image or Qt's QImage, it applies smoothing to the resulting image.
PS: There's already a question that covers this for C#.

Since you have ImageMagick installed, a shell-script friendly option would be mogrify -scale 200% image.png This uses linear resampling when scaling down, and nearest neighbor when scaling up.
If C++ is preferred, the scale method in Image works the same way.

You can use OpenCV resize() to do this. You should use INTER_NEAREST interpolation.

In Qt, the scaled function of the QImage class gets the job done if you use the default parameter for the transformation method which is Qt::FastTransformation.

Related

Most efficient way to blur an image in opencv

I am blurring the background of an image using the blur method. All the tutorials I have seen show the highest kernel size of (7,7). But that is not blurred enough for what I need it for.
I have used Size(33,33) and it works alright but I would like to go higher so currently I am using Size(77,77). Is this the most efficient way of blurring an image in OpenCV? And is it okay to go that high at all?
Another Idea is run the blur method more than once. with a kernel size of (7,7), but that doesn't seem like it is more efficient.
EDIT:
OpenCV version 3.2
Try cv::stackBlur().
It's an addition from v4.7.0. Its performance is almost flat, i.e. independent of kernel size. The pull request contains performance figures: https://github.com/opencv/opencv/pull/20379
GaussianBlur(sigmaX=22) (30 ms)
stackBlur(ksize=(101,101)) (0.4 ms)

Customize my own HOG Object Detection

I want to build my own Object Detection algorithm using HOG feature. Since OpenCV has its own framework to do this for pedestrian detect, I think I can just modify some of the parameters to customize my own one. But I have a few questions about it after reading this.
1.Prepare my own dataset:
Do I have to make all the pos and neg images the same size? Sometimes resize the image may lead to image deformation and affect the hog result. If not, then I have to change the HOG parameters to adapt every image(for example: set the window size = image size and generate a 3780 vector). which one is better.(I prefer the second one)
2.For training the SVM.
In opencv, I think they use SVMLight for training which has been integrated in OpenCV. Can I use Libsvm or other package (compatible with function hog.setSVMDetector())?
3.For hog.detectMultiScale() function
After doing all these above, can I get the result (rectangles) just calling this function?
Thanks!

C++ OpenCV sky image stitching

Some background:
Hi all! I have a project which involves cloud imaging. I take pictures of the sky using a camera mounted on a rotating platform. I then need to compute the amount of cloud present based on some color threshold. I am able to this individually for each picture. To completely achieve my goal, I need to do the computation on the whole image of the sky. So my problem lies with stitching several images (about 44-56 images). I've tried using the stitch function on all and some subsets of image set but it returns an incomplete image (some images were not stitched). This could be because of a lack of overlap of something, I dunno. Also the output image has been distorted weirdly (I am actually expecting the output to be something similar to a picture taken by a fish-eye lense).
The actual problem:
So now I'm trying to figure out the opencv stitching pipeline. Here is a link:
http://docs.opencv.org/modules/stitching/doc/introduction.html
Based on what I have researched I think this is what I want to do. I want to map all the images to a circular shape, mainly because of the way how my camera rotates, or something else that has uses a fairly simple coordinate transformation. So I think I need get some sort of fixed coordinate transform thing for the images. Is this what they call the homography? If so, does anyone have any idea how I can go about my problem? After this, I believe I need to get a mask for blending the images. Will I need to get a fixed mask like the one I want for my homography?
Am I going through a possible path? I have some background in programming but almost none in image processing. I'm basically lost. T.T
"So I think I need get some sort of fixed coordinate transform thing for the images. Is this what they call the homography?"
Yes, the homography matrix is the transformation matrix between an original image and the ideal result. It warps an image in perspective so it can fit in stitching to the other image.
"If so, does anyone have any idea how I can go about my problem?"
Not with the limited information you provided. It would ease the problem a lot if you know the order of pictures (which borders which.. row, column position)
If you have no experience in image processing, I would recommend you use a tutorial covering stitching using more basic functions in detail. There is some important work behind the scenes, and it's not THAT harder to actually do it yourself.
Start with this example. It stitches two pictures.
http://ramsrigoutham.com/2012/11/22/panorama-image-stitching-in-opencv/

'creating' images effectively

I'll first tell you the problem and then I'll tell you my solution.
Problem: I have a blank white PNG image approximately 900x900 pixels. I want to copy circles 30x30 pixels in size, which are essentially circles with a different colour. There are 8 different circles, and placed on the image depending on data values which I've created elsewhere.
Solution: I've used ImageMagicK, it's suppose to be good for general purpose image editing etc. I created a blank image
Image.outimage("900x900","white");
I upload all other small 30x30 pixel images with 'read' function.
I upload the data and extract vales.
I place the small 'circle' images on the blank one using the composite command.
outimage.composite("some file.png",pixelx,pixely,InCompositeOp);
This all works fine and the images come up the way I want them too.
However its painfully SLOW. It takes 20 seconds to do one image, and I have 1000 of them. Surely there must be a better way to do this. I've seen other researchers simulate images way more complex and way faster. It's quite possible I took the wrong approach. Maybe I sould be 'drawing' circles instead of 'pasting' them or something. I'm quite baffled. Any input is appreciated.
I suspect that you just need some library that is capable of drawing circles on bitmap and saving that bitmap as png.
For example my Graphin library: http://code.google.com/p/graphin/
Or some such. With Graphin you can also draw one PNG on surface of another as in your case.
You did not give any information about the platform you are using (only "C++"), so if you are looking for a platform independent solution, the CImg library might be worth a try.
http://cimg.sourceforge.net/
By the way, did you try drawing the circles using the ImageMagick C++ API Magick++ instead of "composing" them? I cannot believe that it is that slow.

How to convert photos to Polaroid-like programmatically?

How to convert modern day photos to the look and feel of those Polaroid photos ? References and/or sample codes are welcome. Thanks!
Convert the images to HSV (cv::cvtColor) then look at adjusting the hue/saturation values
see http://en.wikipedia.org/wiki/HSL_and_HSV for a rather too technical article
Here is a video showing how to do it in GIMP: http://www.youtube.com/watch?v=1LAUm-SrWJA and here is the tutorial: http://howto.nicubunu.ro/gimp_polaroid_photo/
You can look at various steps (each one of them would be some basic image processing operation) and glue together to make your own code. I think each GIMP operation is in turn available as a script-fu script/code.
I would suggest using blend modes along with the HSV conversion.
This website below has been of tremendous help to me while processing images to give them an 'old' look.
http://www.simplefilter.de/en/basics/mixmods.html
Do note that you need to mix and match different blend modes with color tints and blur algorithms to achieve the various Polaroid effects.
A good starting point would be look at ImagemMagick. It already does have cmdline options to change the hue and saturation of a photo. Find a parameter set that gives you the result that you want and look at the source code to see what it is doing behind the scenes..
Programmatically, you'd want to use an image processing library such as OpenCV.
A large part of the effect (besides adding the white frame) is a change in the image color balance and histogram. This is due to the degradation of the chemical elements in the Polaroid film.
The types of operations you would need to apply to the image:
Changing color spaces such as HSV;
Desaturation;
Blending with color filters (this is the suggested way here);
Changing the brightness and contrast of the image channels for the chosen color space.
Obviously, most tutorials about how to do this in Photoshop (or other photo editing apps), can be converted into programs using OpenCV.