tools to rotate window like cvmovewindow - c

there is a command to move a window (probably showing image captured from camera) in OpenCV which is cvMoveWindow. But, is there a command in OpenCV which allows window rotation?
if OpenCV does not have one, is there any library doing so?
I've googled for several days but could not find one

You can't rotate the window itself since OpenCV provides no feature for this task, but you can rotate the image instead.
A solution that might be faster than warpAffine is: call cvTranspose() followed by cvFlip() to rotate the image 90 degrees.
The following code uses the C++ interface of OpencV to demonstrate this operation:
cv::Mat src = imread("image.png", 1);
cv::Mat dst;
cv::transpose(src, dst);
cv::flip(dst, dst, 1);

There is no command in opencv to rotate a window. However you can rotate the image and display it in a window. I think it will be hard to find any library which can do this because it has something to do with OS.
Check this link to do away with image rotation

Related

Resize an image in C / SDL 1.2

I have a 64x64 image and I want to resize it just as I want within my program (make it 32x32 or 86x86 for exemple). How can I do it? I use SDL 1.2 in C.

SDL2 pre-multiplying alpha channel when loading surface on OS X?

I'm loading 32 bit RGBA normalmap textures, with a heightmap encoded in the alpha channel, via SDL2 2.0.7 and SDL2_image 2.0.2 on OS X Sierra.
Every pixel in these textures has a non-zero RGB value, encoding a directional normal vector. A directional vector of (0, 0, 0) (i.e. black) is invalid.
And yet, when I load such a texture via SDL2_image, the areas of the texture with an alpha value of 0 also yield RGB values of 0. I think SDL is perhaps pre-multiplying the alpha value for these pixels?
Attached is one of these normalmap textures. You can confirm it is valid by opening the texture in e.g. GIMP and using the color picker on one of the transparent areas. You'll see that, indeed, the transparent areas still have an RGB color that is blue-ish (an encoded normal vector).
And below is a minimal test case illustrating the issue for the attached PNG file:
#include <SDL_image.h>
#include <assert.h>
int main(int argc, char **argv) {
SDL_Surface *s = IMG_Load("green3_2_nm.png");
assert(s);
for (int i = 0; i < s->w * s->h; i++) {
const Uint32 *in = (Uint32 *) (s->pixels + i * s->format->BytesPerPixel);
SDL_Color color;
SDL_GetRGBA(*in, s->format, &color.r, &color.g, &color.b, &color.a);
assert(color.r || color.g || color.b);
}
SDL_FreeSurface(s);
return 0;
}
I'm compiling this test case with gcc $(pkg-config --cflags --libs sdl2_image) test.c
The assertion on line 15 will fail several rows into the image -- i.e. exactly where the alpha value drops to 0.
I have tried both TGA and PNG image formats, but SDL does the same thing to both of them.
Is this a bug in SDL, or am I missing something? I'm curious if folks see this same issue on other platforms as well.
===
Answer: Core Graphics, the default image loading backend for SDL2_image on Apple OS X, does indeed pre-multiply alpha -- always. The solution is to recompile SDL2_image without Core Graphics support, and instead enable libpng, libjpeg, and any other image codecs you require:
./configure \
--disable-imageio \
--disable-png-shared \
--disable-tif-shared \
--disable-jpg-shared \
--disable-webp-shared
On my system, I had to disable Core Graphics (imageio) and also the shared library loading of the other codecs, as you can see. This produced a fat SDL2_image.so that was statically linked against libpng, libjpg, etc.. but worked as expected.
SDL_image is a wrapper around platform-specific image loading code, rather than using the same image loader on all platforms.
Linux: LibPNG, LibJPEG
macOS, iOS: Core Graphics
Windows: WinCodec
This has the advantage of reducing the size of SDL_image, since it doesn't have to ship with any image decoding code, and can instead link dynamically against something that's likely installed on your system already. However, on macOS and iOS, Core Graphics does not support non-premultiplied alpha, so SDL_image has to reverse it.
See: mac-opengl - Re: kCGImageAlphaFirst not implemented (Was: (no subject)) from May 2007 (from the Wayback machine):
Honestly, I wouldn't expect CGBitmapContextCreate() to support non- premultiplied alpha any time soon.
...
I'm not sure if using ImageIO + CoreGraphics was ever really targetted at being used for an image loading scheme for OpenGL applications.
This behavior was discovered in LibSDL bug #838 - OSX SDL darkens colours proportional to increase in alpha and a workaround was introduced in SDL_image changeset 240.
You can see that the workaround merely un-premultiplies the alpha, this is a horribly lossy process.
To address this, you could build your own version of SDL_image on macOS that uses LibPNG. This should be possible just through configuration, you should not have to make any changes to the SDL_image code itself. To do this, use the --disable-imageio option. SDL_image ships with its own copy of the LibPNG code, so you should not need to install LibPNG in order to get this to work.

Image output in C

Quick question, is there a way to show an image(ex. bmp) from file using C? It's not in graphics.h apparently, and I can't use Allegro because it does not support Borland(or so I've read). I need to use the very old compiler for a school project. I would like to ask if anyone had any experience of doing this using other libraries? If yes, which library was it? Thanks a lot.
I hope you have visual (windows) borland like Borland C++ builder 3++ or turbo C++ not the MS DOS one. in that case it is quite easy because you can use bitmap which is part of VCL so no additional include is needed.
here you can find some hints on rendering under borland
now how to visualize picture from file to your window:
// this will create and load your bitmap
Graphics::TBitmap *bmp=new Graphics::TBitmap;
bmp->LoadFromFile("image.bmp");
bmp->HandleType=bmDIB;
bmp->PixelFormat=pf32bit;
// on paint you can draw your image to form,paintbox,another bitmap or whatever...
Form1->Canvas->Draw(0,0,bmp); // also you can use stretch draw or copy rectangle GDI functions
// before exiting delete the bmp
delete bmp;
[Notes]
You can also save image by bmp->SaveToFile("out.bmp"); In case you need jpg then add:
#include <jpeg.hpp>
TJPEGImage *jpg=new TJPEGImage;
jpg->LoadFromFile("image.jpg");
bmp->Assign(jpg);
delete jpg;
this will load jpg to your bmp also you can save jpg as well in the same way. Beware older Borlands has a bug in TJPEGImage and will crash if the jpg resolution is too big**

How do you turn a .png file into a 2-d matrix?

I am working on a project for a Bio-medical Imaging course (Note: it is a non-programming course, so asking for programming help is not cheating. Asking for conceptual help with planning would be cheating.) where I need to manipulate an image using different mathematical transforms. I am writing in C so it can be as fast as possible. I have finished the code for the mathematical transforms, but I have realized that I do not know how to turn a grayscale .png file into a 2-d matrix/array to compute with, and I do not know how to display a .png file in C. Can anyone help me?
I'm trying to turn the "image.png" image into a 2d array where each entry in the array has a value between 0 - 255 and corresponds with each pixel in "image.png". I also want to turn a 2d array where each entry corresponds to a pixel in the image and has a value between 0 - 255 into a new "image_two.png" file.
I'm a somewhat new programmer. I have a solid base in python programming, but C is new for me. I have done a lot of research and I have found a lot of people talking about using "this library" or "that library", or also "this library", but how do I use a downloaded library in C? It's unfamiliar territory for me as a python programmer :(
I'm using Ubuntu 12.04
To reiterate:
How do you read a grayscale .png image as a 2-d array/matrix in C?
How do you display a 2-d array/matrix as a grayscale image in C?
How do you use a downloaded library in C code (specifically for the two questions above)? I found out how to use these libraries.
EDIT: I am still having trouble figuring out how to create a grayscale 2d array out of a .png file and how to make a .png file out of a grayscale 2d matrix. Can anyone else help?
You can use a more general purpose image handling library and you might find it easier to use. I recommend FreeImage http://freeimage.sourceforge.net/. See the pixel access section of the manual to get access to the pixel data. You can then work with it directly or copy it into your own matrix.
To install a library in Linux, typically you will use the package manager. For example, in Debian (this includes Ubuntu) you might do:
$ apt-cache search libpng
You'll decide which package to install based on the results of running this command and then you will run
$ sudo apt-get install <package-name>
This command will likely install png.h in a location that is already included in gcc's search path. This means that to use png.h in your program, all you have to do is include it.
#include <png.h>
Skip to chapter 3 in the libpng manual for a walkthrough on reading a png file.

What's the easiest way to display an image in C/Linux? [duplicate]

This question already has answers here:
Easy way to display a continuously updating image in C/Linux
(5 answers)
Closed 5 years ago.
I'm quite comfortable with C for numerical computation, but not for graphical programming. I have a Nx by Ny by 3 RGB matrix in a command-line program for linux (gcc, ubuntu) and I want to pop up a window with it as an image. What's the easiest way to do this? It has to be relatively quick because I'll be updating the image frequently.
Thanks
Here is how to display and image with Gtk2 using C. You can find more information (including a tutorial) on the Gtk2 site.
#include <gtk/gtk.h>
void destroy(void) {
gtk_main_quit();
}
int main (int argc, char** argv) {
GtkWidget* window;
GtkWidget* image;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
image = gtk_image_new_from_file(argv[1]);
gtk_signal_connect(GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC (destroy), NULL);
gtk_container_add(GTK_CONTAINER (window), image);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Here is how to compile it:
gcc -Wall img.c -o img `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`
You could try using image magick it seems to have a function for displaying an image to an x window with a minimal amount of work. I have used image magick in the past and have been pleased with it. I have not tried this specific function though.
Display function
The C language itself doesn't have any built-in graphics capability. You can use a graphics toolkit like Qt, gtk, wxWidgets, etc. You could also construct an image file (BMP is pretty simple), and then launch (using fork and exec) an application to view it. Ubuntu uses gnome by default, you could fork "gnome-open" with a command-line argument that is the name of the file you created, and gnome-open will then launch whichever application is associated with the file type.
It sounds like maybe you're doing some scientific computing and just need a quick way to display results.
Something like gnuplot may give you better mileage than learning a generic windowing toolkit. It's a general plotting utility and specifically geared towards displaying data sets.
Draw the image to a pixmap, create an X window (or allocate a framebuffer), and write the pixmap to the window.
Or, look at ImageMagick's implementation of the display command.
If you don't mind learning GTK+, then that might be the most straightforward way. Create a window and a drawable, and make the drawable a child of the window. Create a pixmap of your image and draw it to the drawable. Cairo will simplify this, since you'll only need to tweak your image format to make it work.
The cairo graphics library is also a good way to go, see http://cairographics.org/ . It's cross platform, fairly efficient, and will let you work with Gnome/KDE/Windows or just generating image files fairly simply. It's designed for vectors, so also handles clipping, fonts and scaling well.

Resources