Save an array of IplImage? - opencv

I have a database of images that i processed and saved as:
IplImage* database[10000];
database[0]= image1
database[1]= image2
... etc
Now I want to save this database IplImage matrix. How can i do this ? I know i can loop over all the images and save them one by one, but that is not really what i am looking for.
I read something like cvSave and cvLoad which allows me to save and load in one command but i am getting an error when i use it (cvSave("myimagedatabase.xml",&database);). Can you please guide me ?
Thank you in advance

If you don't want to save them as individual images what about using a sequence of images - also known as a movie?
See opencv cvVideoWriter, if you select 0 as the codec they will be saved uncompressed

What I gather from your question is that you are interested in saving strictly the data to some file, and then reloading back at a later point and that you would not be interested in opening the stored data in an image editing program or something like this.
First thing to consider, what are the important parts of IplImage
* depth
* height
* width
* nChannels
* imageData array
What I would do is in a loop for each IplImage make a function to write each data value into a binary file. Give the file a header noting how many images there are. Using depth, height width and nChannels you can compute the size of the imageData array. Assuming all have the same depth (IPL_DEPTH_8U for example) then this should be easy, if they vary this can get tricky.
To load the data simply read in the binary data and header information and one by one loop through all the data and create new IplImages based on that data.

Related

How to capture Open3d screen without saving

I want to use the open3d image screen right away without saving it as a file.
However, the 'capture_screen_image' function provided by open3d must save the image.
(http://www.open3d.org/docs/release/python_api/open3d.visualization.Visualizer.html)
This causes the problem of having to read the saved image back to 'cv2.imread'.
I wonder if there are other ways to solve this problem or other functions provided by open3d.
I would use capture_screen_float_buffer() instead of capture_screen_image().
Remember the result is a (normalised) float numpy mat, not a typical np.uint8 typed one so depending on the use, you might also need to scale it back up to 0-255 range and cast as np.uint8:
# get the image
o3d_screenshot_mat = visualizer.capture_screen_float_buffer()
# scale and convert to uint8 type
o3d_screenshot_mat = (255.0 * np.asarray(o3d_screenshot_mat)).astype(np.uint8)
# use as required
# cv2.imshow("screenshot", o3d_screenshot_mat) , PIL.Image.fromarray(o3d_screenshot_mat , "RGB"), etc.
(e.g. if you want to visualise, remap to 0-255 range, otherwise leave data as is and simply save/load as needed)

Any solution for TGA format loading in OpenCV4

There's no support TGA format for OpenCV currently.
And I know there's a single header file library named stb_image that allow you to read/write TGA image.
But the use case with OpenCV on the Internet are so few. (more often to see people use it with OpenGL)
The second method I found.
There's a short code included (the answer) in this topic:
Loading a tga/bmp file in C++/OpenGL
Someone use this code to read TGA file into cv::Mat just like the code below.
Tga tgaImg = Tga("/tmp/test.tga");
Mat img(tgaImg.GetHeight(), tgaImg.GetWidth(), CV_8UC4);
memcpy(img.data, tgaImg.GetPixels().data(), tgaImg.GetHeight() * tgaImg.GetWidth() * 4);
But this is only for reading part. I wonder if stb_image can do the same thing like the code above. I mean the image data structure might be different. (not look into them yet)
I would like to ask people who also experience this before. Since DDS/TGA image format are also popular using in game texture, there must be people have already found the way. I mean read/write TGA format in OpenCV code.
Thanks.
For saving opencv image in tga use stbi_write_tga. This function takes pointer to image data as argument, which is img.data in case of cv::Mat type.

Store GIS Quadtree in Raw File (w/ Geohash) or PNG?

I am collecting GIS data consisting of normalized four values for whole world. I am curious on what would be the best way to store this data and wanted to take your advise. Would it be more efficient (in terms of size) to store the four values of the quadtree, along with a Geohash index via Z-order (Morton) or Hilbert curve? Or would it be more efficient to store it in a PNG file using alpha = 0 for empty spaces and lossless compression? The enclosed image 1 only visualizes one of the four values over Google Maps and I need to store this global data each day. Please, note that I will only store leaf nodes as visualized in the image 1 rather than the whole quadtree. I will also store this over time so I would also like to know your ideas about how video compression would improve.
Thank you all in advance for your time and consideration!

Verilog code to calculate number of rows and columns from an input image frame

I am having troubles with a verilog module which is a small part of a very big CMOS camera image code.The module takes in clk and reset as inputs and spits out hsync,vsync,pixclk and pixel data.The code reads in an image and saves it to memory and then does a a bunch of if else statements like :
if (row_count<NUM_ROWS-1) && (col_count< NUM_COLS)
begin
vsync <=1;
hsync <=1 ;
pixe_data <= mem[row_count*NUM_ROWS+col_count];
end
else if
......
Till now the image dimensions were hard coded using NUM_COLS and NUM_ROWS but I am trying to change this such that the code counts the rows and columns of the incoming image on the fly. I have tried using $fscanf,$fgets,$Sscanf etc but I am not getting the right results.In fact I am getting no results at all.My simulation gets stuck or it says its out of memory or fd is a null file descriptor.Also when I convert NUM_ROWS and NUM_COLS to be variables I get an error saying illegal operand for constant operation.
I would much appreciate if some could give me some insight into a different method to approach this problem.
My apologies in advance if the information here is insufficient or unclear.
Thanks
SK
The illegal operand for constraint operations is likely due to the dimension for mem. The dimensions needs to be a constant, e.g. reg [DATA_SIZE-1:0] mem [ROW_MAX*COL_MAX-1:0]; You'll need to decide the max image size you will process. If the max dimensions are not 2**N then you will need to implement some from of protection, e.g. signal an error and not process anything, truncate the image, or something else.
I'm not sure how to find the rows and columns of unknown image dimensions without writing custom a PLI. $fscanf can read in the image data and you will most likely want to use "%u" for format component. If image contains information about its dimentions in the file itself, then you could extract information with the $fscanf or $fread.
Other note, from your original code snip-it:
pixe_data <= mem[row_count*NUM_ROWS+col_count];
should be:
pixe_data <= mem[row_count*NUM_COLS+col_count];
Otherwise there is a risk of accessing the same data from different addresses or something out of range.

OpenCV imwrite increases the size of png image

I am doing image manipulation on the png images. I have the following problem. After saving an image with imwrite() function, the size of the image is increased. For example previously image is 847KB, after saving it becomes 1.20 MB. Here is a code. I just read an image and then save it, but the size is increased. I tried to set compression params but it doesn't help.
Mat image;
image = imread("5.png", -1);
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
compression_params.push_back(0);
imwrite("output.png",image,compression_params);
What could be a problem? Any help please.
Thanks.
PNG has several options that influence the compression: deflate compression level (0-9), deflate strategy (HUFFMAN/FILTERED), and the choice (or strategy for dynamically chosing) for the internal prediction error filter (AVERAGE, PAETH...).
It seems OpenCV only lets you change the first one, and it hasn't a good default value for the second. So, it seems you must live with that.
Update: looking into the sources, it seems that compression strategy setting has been added (after complaints), but it isn't documented. I wonder if that source is released. Try to set the option CV_IMWRITE_PNG_STRATEGY with Z_FILTERED and see what happens
See the linked source code for more details about the params.
#Karmar, It's been many years since your last edit.
I had similar confuse to yours in June, 2021. And I found out sth which might benefit others like us.
PNG files seem to have this thing called mode. Here, let's focus only on three modes: RGB, P and L.
To quickly check an image's mode, you can use Python:
from PIL import Image
print(Image.open("5.png").mode)
Basically, when using P and L you are attributing 8 bits/pixel while RGB uses 3*8 bits/pixel.
For more detailed explanation, one can refer to this fine stackoverflow post: What is the difference between images in 'P' and 'L' mode in PIL?
Now, when we use OpenCV to open a PNG file, what we get will be an array of three channels, regardless which mode that
file was saved into. Three channels with data type uint8, that means when we imwrite this array into a file, no matter
how hard you compress it, it will be hard to beat the original file if it was saved in P or L mode.
I guess #Karmar might have already had this question solved. For future readers, check the mode of your own 5.png.

Resources