stitching manually with opencv - c

Hi I am trying to stitch some images without using the stitch class provided by opencv. But, the output is quit unexpected.
I will explain it with the input and output image.
input1
input2
expected output
real output
I think something is wrong with my ROI copying. anybody please help !!!
My code for stitching part is as follows-
std::vector< Point2f > points1,points2;
for( int i = 0; i < matches1.size(); i++ )
{
points1.push_back( keypoints_input1[matches1[i].queryIdx ].pt );
points2.push_back( keypoints_input2[matches1[i].trainIdx ].pt );
}
/* Find the Homography Matrix for current and next frame*/
Mat H1 = findHomography( points2, points1, CV_RANSAC );
/* Use the Homography Matrix to warp the images*/
cv::Mat result1;
warpPerspective(input2, result1, H1, Size(input2.cols+150, input2.rows+150), INTER_CUBIC);
Mat stitch_1(Size(input2.cols+150, input2.rows+150),CV_8UC3);
Mat roi1(stitch_1, Rect(0, 0, input1.cols, input1.rows));
Mat roi2(stitch_1, Rect(0, 0, result1.cols, result1.rows));
input2.copyTo(roi1);
result1.copyTo(roi2);
Can anybody tell me where I am going wrong ? thanks.
Edit: input1(640,360) and input2(790,510) are of different size.

I hope that this example helps you.
It's interesting to test on different images.
EDIT:
try this code:
Mat stitch_1(Size(input2.cols*2+ input1.rows,input2.rows*2),CV_8UC3);
Mat roi1(stitch_1, Rect(0, 0, input1.cols, input1.rows));
Mat roi2(stitch_1, Rect(0, 0, result1.cols, result1.rows));
result1.copyTo(roi2);
input1.copyTo(roi1);
imshow("final", stitch_1);

Related

CSFML Vertex Array and drawing

It's been a few weeks i've been working a project for my school and I now need to work on particles. I've been looking at vertices and it looks like to be a good way to make them.
I've started by trying to print at least one vertex on the screen and to print it, but I don't know what I'm doing wrong.
CSFML is a very restricted library as not many people use it, so trying to find SFML examples and to figure out the derivates of the functions to C is quite hard and giving me some troubles.
Here's my code :
{
sfVertex a;
sfVector2f apos = {200, 100};
a.color = sfRed;
a.position = apos;
sfVertexArray *array = sfVertexArray_create();
sfVertexArray_setPrimitiveType(array, sfPoints);
sfVertexArray_append(array, a);
sfRenderWindow_drawVertexArray(window, array, 0);
}
In this example, I'm trying to create a vertex, give it a position, a color, and then create a vertex array that takes point vertices and to append my vertex to the vertex array. I think the only problem here is to print it on the screen, as sfRenderWindow_drawVertexArray(window, array, 0); doesn't print anything, and if I put the render state to 1 my program just crashes before even opening my window.
I tried to find examples and explanations about this function but I'm pretty much lost now.
I think your error was that you did not set sfPoints in your code. Here is a simple code that draws 4 points.
#include <iostream>
#include <SFML/Graphics.hpp>
int main(){
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
while (window.isOpen()){
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
}
sf::VertexArray vertexArray (sf::Points, 4);
vertexArray[0].position = sf::Vector2f(10, 10);
vertexArray[1].position = sf::Vector2f(10, 20);
vertexArray[2].position = sf::Vector2f(20, 10);
vertexArray[3].position = sf::Vector2f(20, 20);
// Set colour for all vertices
for(int i = 0; i < 4.; i++){
vertexArray[i].color=sf::Color::Yellow;
}
window.clear();
window.draw(vertexArray);
window.display();
}
return 0;
}

cvWatershed unsupported format or combination of formats

I'm working with OpenCV 2.4.11 in C on Code::Blocks, in particular through the O'Reilly book Learning OpenCV. The section on the watershed algorithm was a bit short, so I thought I'd play with it a bit to see how exactly it works. However, every time I call the function I get the following error:
OpenCV Error: Unsupported format or combination of formats (Only 32-bit, 1-chann
el output images are supported) in cvWatershed
My program so far is very simple:
int main(int arg, int arg2) {
//open windows
cvNamedWindow("Input", 1 );
cvNamedWindow("Markings", 1 );
//load images
IplImage* input = cvLoadImage("ActualDoorPhoto.jpg", CV_LOAD_IMAGE_COLOR);
assert(input != NULL);
IplImage* markingstemp = cvLoadImage("ActualMarkingTest.jpg", CV_LOAD_IMAGE_COLOR);
assert(markingstemp != NULL);
//prepare markings
IplImage* markings = cvCreateImage(cvGetSize(markingstemp), 32, 1);
CvMat* markmat = cvCreateMat(input->width, input->height, CV_32FC1);
cvWatershed(input, markmat);
cvShowImage("Input", input);
cvShowImage("Markings", markings);
cvWaitKey(0);
return 0;
}
I have tried putting both markings and markmat as the second argument for cvWatershed, as well as several other things (notably markings with the contours of markingstemp drawn onto it), but every time I get the same error. Can anyone tell me what I'm doing wrong?
You're inverting the dimensions of the output matrix. It should be:
CvMat* markmat = cvCreateMat(input->height, input->width, CV_32FC1);
The format should also probably be changed to CV_32SC1.

cvCreateFileCapture strange error

i'm trying to create a simple Opencv program in C that creates a file capture from a .avi, and it plays it in a window highlighting faces. I'm running a self-compiled version of Opencv (i already tried the same with a jpeg image and it works).
Building goes well, no errors, no warning, but when i launch it this the console output this:
Unknown parameter encountered: "server role"
Ignoring unknown parameter "server role"
And the program simply stops
Previously it was complaining for a missing /home/#user/.smb/smb.conf file, so i tried installing samba ( even though i've still no idea what does samba have to do in all this )
here is my code:
main(){
printf("Ciao!");
cvNamedWindow("window", CV_WINDOW_AUTOSIZE);
cvWaitKey(0);
printf("ok");
CvCapture* capture = cvCreateFileCapture("monsters.avi");
CvHaarClassifierCascade* cascade = load_object_detector("haarcascade_frontalface_alt.xml");
CvMemStorage* storage = cvCreateMemStorage(0);
//List of the faces
CvSeq* faces;
while (0<10) {
CvArr* image = cvQueryFrame(capture);
double scale = 1;
faces = cvHaarDetectObjects(image,cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(1,1), cvSize(300,300));
int i;
for(i = 0; i < faces->total; i++ )
{
CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i );
cvRectangle( image,
cvPoint(face_rect.x*scale,face_rect.y*scale),
cvPoint((face_rect.x+face_rect.width)*scale,(face_rect.y+face_rect.height)*scale),
CV_RGB(255,0,0) , 3, 8, 0);
}
cvReleaseMemStorage( &storage );
cvShowImage("window", image);
}
cvWaitKey(0);
printf("Ciao!");
}
I thank you for your answer, i switched to C++ for my trials. Now i did this:
int main(){
namedWindow("Video", CV_WINDOW_FREERATIO);
VideoCapture cap("sintel.mp4");
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
for(;;){
Mat frame;
cap>>frame;
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("Video", edges);
//cvWaitKey(0);
}
return(0);
}
Now it succesfully load the video and query a frame, evry time i press a key it obviously query another frame and everything works fine, but if i comment the waitkey() the program simply hangs for a bit and crashes if i try to close the window, i'm starting to think there is a problem with codecs or something like that...
There are so many potential problems in the code, most of them related to not coding defensively.
What is cvWaitKey(0); doing after cvNamedWindow()? It's unecessary, remove it!
What happens if the capture was unsucessful? Code defensively:
CvCapture* capture = cvCreateFileCapture("monsters.avi");
if (!capture)
{
// File not found, handle error and possibly quit the application
}
and you should use this technique for every pointer that you receive from OpenCV, ok?
One of the major problems, is that you allocate memory for CvMemStorage before the loop, but inside the loop you release it, which means that after the first loop iteration there will be no longer a valid CvMemStorage* storage, and that's a HUGE problem.
Either move the allocation procedure to the beginning of the loop, so on every iteration memory is allocated/deallocated, or move the cvReleaseMemStorage( &storage ); call out of the loop.
Now it works fine, i changed cvWaitKey() with this
if(waitKey(30) >= 0) break;
I don't understand exactly why but now everything works as it should :)

converting a vector of keypoints to a CvSeq

I had a quick OpenCV question. Is it possible to take a vector of keypoints and convert it to a CvSeq?
Thanks in advance.
I don't know why you could want that but it should be possible, with these functions you can do whatever you want :
I should add, that the following is a mix of C and C++ (in OpenCV as well)
CreateSeq
CvSeq* cvCreateSeq(int seqFlags, int headerSize, int elemSize, CvMemStorage* storage)
SeqPush
char* cvSeqPush(CvSeq* seq, void* element=NULL)
Here is the code, i have not tried it yet, please let me know if there are errors, if it works or not, i just gave it a try...
vector<KeyPoint> myKeypointVector; //Your KeyPoint vector
// Do whatever you want with your vector
CvMemStorage* storage = cvCreateMemStorage(0)
// By default the flag 0 is 64K
// but myKeypointVector.size()*(sizeof(KeyPoint)+sizeof(CvSeq)) should work
// it may be more efficient but be careful there may have seg fault
// (not sure about the size
CvSeq* myKeypointSeq = cvCreateSeq(0,sizeof(CvSeq),sizeof(KeyPoint),storage);
// Create the seq at the location storage
for (size_t i=0; myKeypointVector.size(); i++) {
int* added = (int*)cvSeqPush(myKeypointSeq,&(myKeypointVector[i]));
// Should add the KeyPoint in the Seq
}
cvClearMemStorage( storage );
cvReleaseMemStorage(&storage);
Julien,

opencv save file xml

I hope that you can help me...
I have an IplImage (reimg_right) 320 X 240, IPL_DEPTH_32F
and I want to save it as an image and as an xml file.
I use this code:
sprintf(name1,"path1/image.bmp");
sprintf(name2,"path2/feature_image32F.xml");
cvSaveImage(name1,reimg_right);
cvSave(name2, reimg_right, NULL, NULL, cvAttrList(0,0));
all is ok but the problem is that in the xml file I don't have a matrix 320 X 240 but a matrix 19200 X 4 !!!
someone knows how to hold the dimensions?
thanks gabriele
I don't know what OpenCV version you are using, but some time ago you could do just:
cvSave("file.xml", my_img);
assuming my_img as:
CvMat* my_img = cvCreateMat(320, 240, CV_32FC1);
But since you are using an IplImage, you could convert them like:
CvMat mat;
CvMat* my_img = cvGetMat(reimg_right, &mat);
cvSave("file.xml", my_img);

Resources