bufferimage() in processing - save

I'm doing a sketch that gets part of the canvas:
PImage img = get(180, 0, 620, 400);
and the I want to upload this to my web server with these functions I had found:
http://wiki.processing.org/w/Saving_files_to_a_web-server
The thing is that when I try to call the function saveToWeb_saveJPG(), the function converts the PImage to a Byte array with bufferImage(), and processing says it does not exist. How could I convert the PImage variable to a Byte array?

The method requires a byte array or byte[]. You can access this from your PImage img like this:
img.pixels;

I tryed this way:
PImage img = loadImage("file.ext");
byte[] toSave = new byte[img.width * img.height];
PImage.loadPixels();
for(int i = 0; i < img.width * img.height; i ++)
toSave[i] = (byte)img.pixels[i];
img.updatePixels();
It doesn't present any issue, in compilation neither running!
I think it just work even though you can't open it as image if you save it as file!

Easiest way to satisfies your question:
bufferImage(get(180, 0, 620, 400))
So your code will look similar to this:
saveToWeb_saveJPG("mytitle","savedJPG",bufferImage(get(180, 0, 620, 400)));

Related

Imagemagick c api : How to read png image frmaes from gif file , what calls to use

So I created gif file from 5 png files with using MagickCoalesceImages call and store it on disk.
How I can read these files back from gif file ?
MagickReadImage does not help
Hard to help without seeing the code, but I can assume you created 5 images with something like...
MagickWand
* gif2png;
gif2png = NewMagickWand();
MagickReadImage(gif2png, "input.gif");
MagickWriteImages(gif2png, "output_%02d.png", MagickFalse);
gif2png = DestroyMagickWand(gif2png);
How I can read these files back from gif file?
You would use MagickReadImage to decode the image from the file, and MagickAddImage to append the decoded image onto a image-stack.
MagickWand
* png2gif,
* temp;
// Create a blank image-stack.
png2gif = NewMagickWand();
char filename[PATH_MAX]; // PATH_MAX provided by limits.h
// Iterate over images to append.
for (int i = 0; i < 5; ++i) {
sprintf(filename, "output_%02d.png", i);
// Read image from disk.
temp = NewMagickWand();
MagickReadImage(temp, filename);
// Add "frame" to stack.
MagickAddImage(png2gif, temp);
temp = DestroyMagickWand(temp);
}
MagickWriteImages(png2gif, "output.gif", MagickTrue);
png2gif = DestroyMagickWand(png2gif);
Warning: The above example omits basic error handling, and assumes the filename names are a sequential series.
Update
From the comments, if you wish to extract a single frame as a PNG file, there are a few ways.
Fastest way is to use MagickWriteImages
MagickWriteImages(img, "output_%02d.png", MagickFalse);
Or use the image stack iterators.
for (MagickSetFirstIterator(img); MagickHasNextImage(img); MagickNextImage(img)) {
MagickWriteImage(img, "output_%02d.png");
}
Or, if the PNG filenames are defined, and you need to map them.
const char * filenames[5] = {
"first.png",
"second.png",
"third.png",
"forth.png",
"fifth.png"
};
for (int i = 0; i < 5; ++i) {
MagickSetIteratorIndex(img, i);
MagickWriteImage(img, filenames[i]);
}
Without seeing the code, we can't offer much help, and can only guess what an acceptable solution would be.

MatToQImage QPixmap::scaled: Pixmap is a null pixmap

I Try to display a video processed by OpenCV, which is in Mat format, to a Qt self-defined FrameLabel. Mat frames can be populated when I add the OpenCV function imshow("frame",mat), but turn to the QPixmap::scaled: Pixmap is a null pixmap after I removed the imshow("frame",mat). Even trying to lock the thread not solve the problem. I have done some search, found that it might due to QPixmap resource should be defined in something like "xxx.qrc".
My code is as below:
void IntelligentSurveillance::on_pushButton_clicked(){
QMutex processingMutex;
string filename = VIDEO_PATH;
VideoCapture cap;
cap.open(filename);
Mat mat;
QImage qImage;
for (;;)
{
cap >> mat;
//processingMutex.lock();
qImage = MatToQImage(mat);
//processingMutex.unlock();
ui.frame->setPixmap(QPixmap::fromImage(qImage).scaled(ui.frame->width(), ui.frame->height(), Qt::IgnoreAspectRatio));
//imshow("frame", mat);
if (waitKey(30) >= 0) break;
}}
Output always like: QPixmap::scaled: Pixmap is a null pixmap
The problem is that it works fine when I add imshow("frame",mat)...
Can anyone give some help? Thanks!!
You need to use temp variable to save image, for example
QPixmap icon = QPixmap(":/img/" + iconFileName);
QPixmap tmp = icon.scaled(30, 30, Qt::KeepAspectRatio);
value = tmp;
And this message gone!

How do I get the RGB values of an Image?

I am taking a computer graphics class, and I need to work with textures, but I can't use any library to do it. I am stuck on loading the rgb values of the images I need to use (the images can be in any format, jpg, raw, png, etc..) so my question is, which is the easiest way to get the rgb values of an image (of any format) without using any libraries to get this values?? Here is what I found already on the site:
unsigned char *data;
File *file;
file = fopen("image.png", "r");//
data = (unsigned char *)malloc(TH*TV*3); //TH and TV are both 50
fread(data, TH*TV*3, 1, file);
fclose(file);
int i;
for(i=0;i<TH*TV*3;i++){
//suposing I have a struct RGB for the rgb values
RGB.r = data[?];// how do I get the r value
RGB.g = data[?];// how do I get the g value
RGB.b = data[?];// how do I get the b value
}
Thanks
Rather than iterating through every byte that you read in, you want to iterate every pixel which consists of 3 bytes. So replace i++ with i+=3.
for(i=0;i<TH*TV*3;i+=3){
RGB.r = data[i];
RGB.g = data[i+1];
RGB.b = data[i+2];
}
Try to use some framework like OpenCV there are several options to get the colors or to manipulate an image.
Here I found this example code:
cv::Mat img = cv::imread("lenna.png");
for(int i=0; i<img.rows; i++) {
for(int j=0; j<img.cols; j++) {
// You can now access the pixel value with cv::Vec3b
std::cout << img.at<cv::Vec3b>(i,j)[0] << " ";
str::cout << img.at<cv::Vec3b>(i,j)[1] << " ";
str::cout << img.at<cv::Vec3b>(i,j)[2] << std::endl;
}
}
But please note that the code above is not very performance, but the code above should give you an idea how to read the pixels.

Compile error in CV_MAT_ELEM

As a result of a call to estimateRigidTransform() I get a cv::Mat object named "trans". To retrieve its contained matrix I try to access its elements this way:
for (i=0; i<2; i++) for (j=0; j<3; j++)
{
mtx[j][i]=CV_MAT_ELEM(trans,double,i,j);
}
Unfortunately with VS2010 I get a compiler error
error C2228: left of '.ptr' must have class/struct/union
for the line with CV_MAT_ELEM. When I unwrap this macro I find something like
(mat).data.ptr + (size_t)(mat).step*(row) + (pix_size)*(col))
When I remove the ".ptr" behind (mat).data it compiles. But I can't imagine that's the solution (or can't imagine that's a bug and I'm the only one who noticed it). So what could be wrong here really?
Thanks!
You don't access the mat elements like this. For a traversal see my other answer here with sample code:
color matrix traversal
or see the opencv refman for grayscale Mat:
Mat M; // should be grayscale
int cols = M.cols, rows = M.rows;
for(int i = 0; i < rows; i++)
{
const double* Mi = M.ptr<double>(i);
for(int j = 0; j < cols; j++)
{
Mi[j]; // is the matrix element.
}
}
Just an addendum from my side: meanwhile I found CV_MAT_ELEM expects a structure CvMat (OpenCV-C-interface) but not cv::Mat (the C++-interface). That's why I get this funny error. Conversion from cv::Mat to CvMat can be done simply by casting to CvMat. Funny confusion with the C and C++ interfaces in OpenCV...

cvExtractSURF don't work when useProvidedKeypoints = true

So, I'm trying to extract some SURF keypoints, but I want to impose these key points! So, I put the last parameter to "true" which is "useProvidedKeypoints".
Also, when I create my Keypoint, I used the default constructor (so some default values there). I only change the point "pt" and the octave that I set to 3.
I'm using the C++ interface with SURF. But I know that the problem is right at cvExtractSURF because I copied that part of the code in mine to help me debug.
When I call that function, with the last parameter set to true, I got this error:
OpenCV Error: Bad argument (Unknown array type) in cvarrToMat, file /home/widgg/opencv/trunk/modules/core/src/matrix.cpp, line 651
terminate called after throwing an instance of 'cv::Exception'
what(): /home/widgg/opencv/trunk/modules/core/src/matrix.cpp:651: error: (-5) Unknown array type in function cvarrToMat
I really don't know what I'm doing wrong!
EDIT:
Here's some code. First how I create the keypoints (I left a couple of informations, like the layer_id stuff, but you get the main idea):
for (json_pt_info_vector::iterator b_beg = beg->points.begin(); b_beg != b_end; ++b_beg)
{
int layer_id = b_beg->layer_id;
json_point_info_coord &jpic = b_beg->coord;
jpic.feature_id = features[layer_id].keypoints.size();
KeyPoint kp;
kp.octave = 3;
kp.pt.x = jpic.x;
kp.pt.y = jpic.y;
features[layer_id].keypoints.push_back(kp);
}
Here's the call to SURF:
SURF surf(300, 3, 4);
for (int i = 0; i < nb_img; ++i)
{
debug_msg("extract_features #4.1");
cv::detail::ImageFeatures &cdif = features[i];
Mat gray_image = imread(param.layer_images[i], 0); // 0 = force to gray scale!
debug_msg("extract_features #4.2");
vector<float> descriptors;
debug_msg("extract_features #4.3");
surf(gray_image, Mat(), cdif.keypoints, descriptors, true); // MUST BE TRUE TO FORCE THE PROVIDED KEYPOINTS
debug_msg("extract_features #4.4");
cdif.descriptors = Mat(descriptors, true).reshape(1, (int)cdif.keypoints.size());
debug_msg("extract_features #4.5");
gray_image.release();
debug_msg("extract_features #4.6");
images[i] = imread(param.layer_images[i]); // keep the image open
}
It crashes after #4.3 in the debug message!
Hope that helps!
EDIT 2:
I replaced some part by cv::SurfDescriptorExtracter. I replace everything from 4.3 to 4.5 with the following line:
extractor.compute(gray_image, cdif.keypoints, cdif.descriptors);
So now, there's still a bug, but it's located somewhere else, not necessary related to this question!
I'm surprised that the call to surf(gray_image, Mat(), cdif.keypoints, descriptors, true) even compiles. the descriptors argument should be a cv::Mat, not a vector.

Resources