placing two images side by side, opencv 2.3, c++ - opencv

I #m reading two images and want to get third one which is just combination of two.
img_object and img_scene don't have same size.
int main( int argc, char** argv )
Mat combine;
Mat img_object = imread( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
Mat img_scene = imread( scene_filename , CV_LOAD_IMAGE_GRAYSCALE );
if( !img_object.data || !img_scene.data )
{ std::cout<< " --(!) Error reading images " << std::endl; return -1; }
namedWindow( "Display window oject", 0 );// Create a window for display.
namedWindow( "Display window scene ", 0 );
namedWindow( "Display window combine ", 0 );
imshow( "Display window oject", img_object );
imshow( "Display window scene", img_scene );
imshow( "Display window scene", combine );
waitKey(0);
return 0;
}

There is a very simple way of displaying two images side by side. The following function can be used which is provided by opencv.
Mat image1, image2;
hconcat(image1,image2,image1);//Syntax-> hconcat(source1,source2,destination);
This function can also be used to copy a set of columns from an image to another image.
Mat image;
Mat columns=image.colRange(20,30);
hconcat(image,columns,image);

// --------------------------------------------------------------
// Function to draw several images to one image.
// Small images draws into cells of size cellSize.
// If image larger than size of cell ot will be trimmed.
// If image smaller than cellSize there will be gap between cells.
// --------------------------------------------------------------
char showImages(string title, vector<Mat>& imgs, Size cellSize)
{
char k=0;
namedWindow(title);
float nImgs=imgs.size();
int imgsInRow=ceil(sqrt(nImgs)); // You can set this explicitly
int imgsInCol=ceil(nImgs/imgsInRow); // You can set this explicitly
int resultImgW=cellSize.width*imgsInRow;
int resultImgH=cellSize.height*imgsInCol;
Mat resultImg=Mat::zeros(resultImgH,resultImgW,CV_8UC3);
int ind=0;
Mat tmp;
for(int i=0;i<imgsInCol;i++)
{
for(int j=0;j<imgsInRow;j++)
{
if(ind<imgs.size())
{
int cell_row=i*cellSize.height;
int cell_col=j*cellSize.width;
imgs[ind].copyTo(resultImg(Range(cell_row,cell_row+tmp.rows),Range(cell_col,cell_col+tmp.cols)));
}
ind++;
}
}
imshow(title,resultImg);
k=waitKey(10);
return k;
}

If the images are not the same size, combine's width will be equal to the sum of the widths, but the height must be the bigger of the heights of the two images.
Define the combination image like this:
Mat combine(max(img_object.size().height, img_scene.size().height), img_object.size().width + img_scene.size().width, CV_8UC3);
Note that we're just creating a new Mat object with height equal to the maximum height and width equal to the combined width of the pictures (if you need a small margin between the pictures, you need to account for that here).
Then, you can define regions of interest for each side inside combine (using a convenient Matconstructor), and finally copy each image to the corresponding side (here I assume the object goes on the left and the scene goes on the right):
Mat left_roi(combine, Rect(0, 0, img_object.size().width, img_object.size().height));
img_object.copyTo(left_roi);
Mat right_roi(combine, Rect(img_object.size().width, 0, img_scene.size().width, img_scene.size().height));
img_scene.copyTo(right_roi);
Edit: Fixed the typo that TimZaman pointed out.

You can do this with a loop, supposing that your images have the same size :
Mat combine = Mat::zeros(img_object.rows,img_object.cols *2,img_object.type());
for (int i=0;i<combine.cols;i++) {
if (i < img_object.cols) {
combine.col(i) = img_object.col(i);
} else {
combine.col(i) = img_scene.col(i-img_object.col);
}
}
I didn't tested it, but that's the way you can do this

I have tried to put multiple images side by side, just try this.
Mat combine = Mat::zeros(img_buff[0].rows,
img_buff[0].cols * (int)img_index.size(), img_buff[0].type());
int cols = img_buff[0].cols;
for (int i=0;i<combine.cols;i++) {
int fram_index = i / img_buff[0].cols;
cout<<fram_index<<endl;
img_buff[fram_index].col(i % cols).copyTo(combine.col(i));
}
imshow("matching plot", combine);
Please pay attention, when you copy columns form one image to another do this:
A.row(j).copyTo(A.row(i));
Don't do this:
A.row(j) = A.row(i);

Related

normalize and convertScaleAbs insights in opencv

Mat img = imread("/home/akash/Desktop/coding/IP/openCV/chessBoard.jpg",1);
Mat gray;
int thresh = 200;
void corner_detect(int,void *){
Mat dst = Mat::zeros(gray.size(),CV_32FC1);
Mat dst_norm,dst_scale;
cornerHarris(gray,dst,2,3,0.04);
normalize(dst,dst_norm,0,255,NORM_MINMAX,CV_32FC1,Mat()); //????
convertScaleAbs(dst_norm,dst_scale); //????
namedWindow("dst_norm",CV_WINDOW_AUTOSIZE);
imshow("dst_norm",dst_norm);
for(int i=0;i<dst_norm.rows;i++){
for(int j=0;j<dst_norm.cols;j++){
if(dst_norm.at<float>(i,j) > thresh){
circle(dst_scale,Point(j,i),5,Scalar(0),2);
}
}
}
imshow("window",dst_scale);
}
int main(){
namedWindow("window",CV_WINDOW_AUTOSIZE);
namedWindow("input",CV_WINDOW_AUTOSIZE);
cvtColor(img,gray,CV_BGR2GRAY);
createTrackbar("threshold","window",&thresh,255,corner_detect);
corner_detect(0,0);
imshow("input",img);
waitKey(0);
return 0;
}
I have taken this code from here which is basically corner detection and drawing circles around it.
I want to ask(where "????" is mentioned in code) working of normalize and convertScaleAbs. I have read the docs but I am still in doubt.I also outputted the dst_norm but it helped me none.
I got that normalize is used to change the value range in array and convertScaleAbs is converting CV_32FC1 type image to CV_8UC1.
But i am unable to understand any insights(i.e. how i got dst_norm and dst_scale when i outputted them).
Any help would be appreciated....
screen shot for reference

OpenCV Fullscreen Windows on Multiple Monitors

I have an OpenCV application that displays a fullscreen window, via:
cv::namedWindow("myWindow", CV_WINDOW_NORMAL)
cv::setWindowProperties("myWindow", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN)
It works fine, but when I have multiple monitors it always displays the fullscreen window on the First monitor. Is there any way to display on the 2nd monitor? I've tried setting X/Y and Width/Height, but they seem to be ignored once fullscreen is enabled.
Edits:
Sometimes pure OpenCV code cannot do a fullscreen window on a dual display. Here is a Qt way of doing it:
#include <QApplication>
#include <QDesktopWidget>
#include <QLabel>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDesktopWidget dw;
QLabel myLabel;
// define dimension of the second display
int width_second = 2560;
int height_second = 1440;
// define OpenCV Mat
Mat img = Mat(Size(width_second, height_second), CV_8UC1);
// move the widget to the second display
QRect screenres = QApplication::desktop()->screenGeometry(1);
myLabel.move(QPoint(screenres.x(), screenres.y()));
// set full screen
myLabel.showFullScreen();
// set Qimg
QImage Qimg((unsigned char*)img.data, img.cols, img.rows, QImage::Format_Indexed8);
// set Qlabel
myLabel.setPixmap(QPixmap::fromImage(Qimg));
// show the image via Qt
myLabel.show();
return app.exec();
}
Don't forget to configure the .pro file as:
TEMPLATE = app
QT += widgets
TARGET = main
LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui
# Input
SOURCES += main.cpp
And in terminal compile your code as:
qmake
make
Original:
It is possible.
Here is a working demo code, to show a full-screen image on a second display. Hinted from How to display different windows in different monitors with OpenCV:
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main ( int argc, char **argv )
{
// define dimension of the main display
int width_first = 1920;
int height_first = 1200;
// define dimension of the second display
int width_second = 2560;
int height_second = 1440;
// move the window to the second display
// (assuming the two displays are top aligned)
namedWindow("My Window", CV_WINDOW_NORMAL);
moveWindow("My Window", width_first, height_first);
setWindowProperty("My Window", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
// create target image
Mat img = Mat(Size(width_second, height_second), CV_8UC1);
// show the image
imshow("My Window", img);
waitKey(0);
return 0;
}
I've tried different ways to make it working, but unfortunetely it seems that this is not possible using OpenCV. The only thing you can do is probably display one window on main(primary) screen just using your current code and handle second window manually - set window position, resize image, and just use imshow function to display it. Here is some example:
void showWindowAlmostFullscreen(cv::Mat img, std::string windowTitle, cv::Size screenSize, cv::Point screenZeroPoint)
{
screenSize -= cv::Size(100, 100); //leave some place for window title bar etc
double xScallingFactor = (float)screenSize.width / (float)img.size().width;
double yScallingFactor = (float)screenSize.height / (float)img.size().height;
double minFactor = std::min(xScallingFactor, yScallingFactor);
cv::Mat temp;
cv::resize(img, temp, cv::Size(), minFactor, minFactor);
cv::moveWindow(windowTitle, screenZeroPoint.x, screenZeroPoint.y);
cv::imshow(windowTitle, temp);
}
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat img1 = cv::imread("D:\\temp\\test.png");
cv::Mat img2;
cv::bitwise_not(img1, img2);
cv::namedWindow("img1", CV_WINDOW_AUTOSIZE);
cv::setWindowProperty("img1", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
cv::namedWindow("img2");
while(cv::waitKey(1) != 'q')
{
cv::imshow("img1", img1);
cv::setWindowProperty("img1", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
showWindowAlmostFullscreen(img2, "img2", cv::Size(1366, 768), cv::Point(260, 1080));
}
}
and the result:
Screen size and screen zero point (i don't know whether this is a correct name of this point - generally it's just a point in which there is screen (0,0) point) you can get using some other library or from windows control panel. Screen zero point will display when you will start moving screen:
If you use QT for writing your code, you can possibly utilize QT5's "Widget".
Here is a tutorial that will show you how to display an OpenCV image in a QT Widget.
Once you have that working you can then use something like this:
QScreen *screen = QGuiApplication::screens()[1]; // specify which screen to use
SecondDisplay secondDisplay = new SecondDisplay(); // your widget
** Add your code to display opencv image in widget here **
secondDisplay->move(screen->geometry().x(), screen->geometry().y());
secondDisplay->resize(screen->geometry().width(), screen->geometry().height());
secondDisplay->showFullScreen();
(Code found here on another SO answer)
I have not tried this myself, so I can't guarantee it will work, however, but it seems likely (if not a little overkill)
Hope this helps.

How to stop a for loop (OpenCV)

I am using Processing (processing.org) for a project that requires face tracking. The problem now is that the program is going to run out of memory because of a for loop. I want to stop the loop or at least solve the problem of running out of memory. This is the code.
import hypermedia.video.*;
import java.awt.Rectangle;
OpenCV opencv;
// contrast/brightness values
int contrast_value = 0;
int brightness_value = 0;
void setup() {
size( 900, 600 );
opencv = new OpenCV( this );
opencv.capture( width, height ); // open video stream
opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT ); // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"
// print usage
println( "Drag mouse on X-axis inside this sketch window to change contrast" );
println( "Drag mouse on Y-axis inside this sketch window to change brightness" );
}
public void stop() {
opencv.stop();
super.stop();
}
void draw() {
// grab a new frame
// and convert to gray
opencv.read();
opencv.convert( GRAY );
opencv.contrast( contrast_value );
opencv.brightness( brightness_value );
// proceed detection
Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );
// display the image
image( opencv.image(), 0, 0 );
// draw face area(s)
noFill();
stroke(255,0,0);
for( int i=0; i<faces.length; i++ ) {
rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
}
}
void mouseDragged() {
contrast_value = (int) map( mouseX, 0, width, -128, 128 );
brightness_value = (int) map( mouseY, 0, width, -128, 128 );
}
Thank you!
A few points...
1 As George mentioned in the comments, you can reduce the size of the capture area, which will exponentially reduce the amount of RAM that your sketch is using to analyze the face tracking. Try making two global variables called CaptureWidth and CaptureHeight and set them to 320 and 240 - which is totally sufficient for this.
2 You can increase the amount of memory that your sketch uses by default in the Java Virtual Machine. Processing defaults to 128 I think, but if you go to the Preferences, you will see a checkbox to "Increase maximum available memory to [x]" ... I usually make mine 1500 mb, but it depends on your machine what you can handle. Dont try to make it bigger than 1800mb unless you are on a 64-bit machine and are using Processing 2.0 in 64-bit mode...
3 To actually break the loop... use the 'break' command http://processing.org/reference/break.html ... but please understand why you want to use that first, as this will simply jump you out of your loop.
4 If you only want to show a certain number of faces, you can test if faces[i] == 1, et cetera, which might help....
But I think the loop itself isn't the culprit here, it's more likely the memory footprint. Start with suggestions 1 & 2 and report back...

OpenCV C++/Obj-C: goodFeaturesToTrack inside specific blob

Is there a quick solution to specify the ROI only within the contours of the blob I'm intereseted in?
My ideas so far:
Using the boundingRect, but it contains too much stuff I don't want to analyse.
Applying goodFeaturesToTrack to the whole image and then loop through the output coordinates to eliminate the once outside my blobs contour
Thanks in advance!
EDIT
I found what I need: cv::pointPolygonTest() seems to be the right thing, but I'm not sure how to implement it …
Here's some code:
// ...
IplImage forground_ipl = result;
IplImage *labelImg = cvCreateImage(forground.size(), IPL_DEPTH_LABEL, 1);
CvBlobs blobs;
bool found = cvb::cvLabel(&forground_ipl, labelImg, blobs);
IplImage *imgOut = cvCreateImage(cvGetSize(&forground_ipl), IPL_DEPTH_8U, 3);
if (found) {
vb::CvBlob *greaterBlob = blobs[cvb::cvGreaterBlob(blobs)];
cvb::cvRenderBlob(labelImg, greaterBlob, &forground_ipl, imgOut);
CvContourPolygon *polygon = cvConvertChainCodesToPolygon(&greaterBlob->contour);
}
"polygon" contains the contour I need.
goodFeaturesToTrack is implemented this way:
- (std::vector<cv::Point2f>)pointsFromGoodFeaturesToTrack:(cv::Mat &)_image
{
std::vector<cv::Point2f> corners;
cv::goodFeaturesToTrack(_image,corners, 100, 0.01, 10);
return corners;
}
So next I need to loop through the corners and check each point with cv::pointPolygonTest(), right?
You can create a mask over your interest region:
EDIT
How to make a mask:
Make a mask;
Mat mask(origImg.size(), CV_8UC1);
mask.setTo(Scalar::all(0));
// here I assume your contour is extracted with findContours,
// and is stored in a vector<vector<Point>>
// and that you know which contour is the blob
// if it's not the case, use fillPoly instead of drawContour();
Scalar color(255,255,255); // white. actually, it's monchannel.
drawContours(mask, contours, contourIdx, color );
// fillPoly(Mat& img, const Point** pts, const int* npts,
// int ncontours, const Scalar& color)
And now you're ready to use it. BUT, look carefully at the result - I have heard about some bugs in OpenCV regarding the mask parameter for feature extractors, and I am not sure if it's about this one.
// note the mask parameter:
void goodFeaturesToTrack(InputArray image, OutputArray corners, int maxCorners,
double qualityLevel, double minDistance,
InputArray mask=noArray(), int blockSize=3,
bool useHarrisDetector=false, double k=0.04 )
This will also improve the speed of your aplication - goodFeaturesToTrack eats a hoge amount of time, and if you apply it only on a smaller image, the overall gain is significant.

mouse handler in opencv for large images, wrong x,y coordinates?

i am using images that are 2048 x 500 and when I use cvShowImage, I only see half the image. This is not a big deal because the interesting part is on the top half of the image. Now, when I use the mouseHandler to get the x,y coordinates of my clicks, I noticed that the coordinate for y (the dimension that doesnt fit in the screen) is wrong.
It seems OpenCV think this is the whole image and recalibrates the coordinate system although we are only effectively showing half the image.
I would need to know how to do 2 things:
- display a resized image that would fit in the screen
get the proper coordinate.
Did anybody encounter similar problems?
Thanks!
Update: it seems the y coordinate is divided by 2 of what it is supposed to be
code:
EXPORT void click_rect(uchar * the_img, int size_x, int size_y, int * points)
{
CvSize size;
size.height = size_y ;
size.width = size_x;
IplImage * img;
img = cvCreateImageHeader(size, IPL_DEPTH_8U, 1);
img->imageData = (char *)the_img;
img->imageDataOrigin = img->imageData;
img1 = cvCreateImage(cvSize((int)((size.width)) , (int)((size.height)) ),IPL_DEPTH_8U, 1);
cvNamedWindow("mainWin",CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);
cvSetMouseCallback( "mainWin", mouseHandler_rect, NULL );
cvShowImage("mainWin", img1 );
//// wait for a key
cvWaitKey(0);
points[0] = x_1;
points[1] = x_2;
points[2] = y_1;
points[3] = y_2;
//// release the image
cvDestroyWindow("mainWin");
cvReleaseImage(&img1 );
cvReleaseImage(&img);
}
You should create a window with the CV_WINDOW_KEEPRATIO flag instead of the CV_WINDOW_AUTOSIZE flag. This temporarily fixes the problem with your y values being wrong.
I use OpenCV2.1 and visual studio C++ compiler. I fix this problem with another flag CV_WINDOW_NORMAL and work properly and returns correct coordinates, this flag enables you to resize the image window.
cvNamedWindow("Box Example", CV_WINDOW_NORMAL);
I am having the same problem with OpenCV 2.1 using it with Windows and mingw compiler. It took me forever to find out what was wrong. As you describe it, cvSetMouseCallback gets too large y coordinates. This is apparently due to the image and the cvNamedWindow it is shown in being bigger than my screen resolution; thus I cannot see the bottom of the image.
As a solution I resize the images to a fixed size, such that they fit on the screen (in this case with resolution 800x600, which can be any other values:
// g_input_image, g_output_image and g_resized_image are global IplImage* pointers.
int img_w = cvGetSize(g_input_image).width;
int img_h = cvGetSize(g_input_image).height;
// If the height/width ratio is greater than 6/8 resize height to 600.
if (img_h > (img_w*6)/8) {
g_resized_image = cvCreateImage(cvSize((img_w*600)/img_h, 600), 8, 3);
}
// else adjust width to 800.
else {
g_resized_image = cvCreateImage(cvSize(800, (img_h*800)/img_w), 8, 3);
}
cvResize(g_output_image, g_resized_image);
Not a perfect solution, but works for me...
Cheers,
Linus
How are you building the window? You are not passing CV_WINDOW_AUTOSIZE to cvNamedWindow(), are you?
Share some source, #Denis.

Resources