Saving output image after performing grabcut using openCV c++ sample code - c++

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
static void help()
{
cout << "\nThis program demonstrates GrabCut segmentation -- select an object in a region\n"
"and then grabcut will attempt to segment it out.\n"
"Call:\n"
"./grabcut <image_name>\n"
"\nSelect a rectangular area around the object you want to segment\n" <<
"\nHot keys: \n"
"\tESC - quit the program\n"
"\tr - restore the original image\n"
"\tn - next iteration\n"
"\n"
"\tleft mouse button - set rectangle\n"
"\n"
"\tCTRL+left mouse button - set GC_BGD pixels\n"
"\tSHIFT+left mouse button - set GC_FGD pixels\n"
"\n"
"\tCTRL+right mouse button - set GC_PR_BGD pixels\n"
"\tSHIFT+right mouse button - set GC_PR_FGD pixels\n" << endl;
}
const Scalar RED = Scalar(0,0,255);
const Scalar PINK = Scalar(230,130,255);
const Scalar BLUE = Scalar(255,0,0);
const Scalar LIGHTBLUE = Scalar(255,255,160);
const Scalar GREEN = Scalar(0,255,0);
const int BGD_KEY = EVENT_FLAG_CTRLKEY;
const int FGD_KEY = EVENT_FLAG_SHIFTKEY;
static void getBinMask( const Mat& comMask, Mat& binMask )
{
if( comMask.empty() || comMask.type()!=CV_8UC1 )
CV_Error( Error::StsBadArg, "comMask is empty or has incorrect type (not CV_8UC1)" );
if( binMask.empty() || binMask.rows!=comMask.rows || binMask.cols!=comMask.cols )
binMask.create( comMask.size(), CV_8UC1 );
binMask = comMask & 1;
}
class GCApplication
{
public:
enum{ NOT_SET = 0, IN_PROCESS = 1, SET = 2 };
static const int radius = 2;
static const int thickness = -1;
void reset();
void setImageAndWinName( const Mat& _image, const string& _winName );
void showImage() const;
void mouseClick( int event, int x, int y, int flags, void* param );
int nextIter();
int getIterCount() const { return iterCount; }
private:
void setRectInMask();
void setLblsInMask( int flags, Point p, bool isPr );
const string* winName;
const Mat* image;
Mat mask;
Mat bgdModel, fgdModel;
uchar rectState, lblsState, prLblsState;
bool isInitialized;
Rect rect;
vector<Point> fgdPxls, bgdPxls, prFgdPxls, prBgdPxls;
int iterCount;
};
void GCApplication::reset()
{
if( !mask.empty() )
mask.setTo(Scalar::all(GC_BGD));
bgdPxls.clear(); fgdPxls.clear();
prBgdPxls.clear(); prFgdPxls.clear();
isInitialized = false;
rectState = NOT_SET;
lblsState = NOT_SET;
prLblsState = NOT_SET;
iterCount = 0;
}
void GCApplication::setImageAndWinName( const Mat& _image, const string& _winName )
{
if( _image.empty() || _winName.empty() )
return;
image = &_image;
winName = &_winName;
mask.create( image->size(), CV_8UC1);
reset();
}
void GCApplication::showImage() const
{
if( image->empty() || winName->empty() )
return;
Mat res;
Mat binMask;
if( !isInitialized )
image->copyTo( res );
else
{
getBinMask( mask, binMask );
image->copyTo( res, binMask );
}
vector<Point>::const_iterator it;
for( it = bgdPxls.begin(); it != bgdPxls.end(); ++it )
circle( res, *it, radius, BLUE, thickness );
for( it = fgdPxls.begin(); it != fgdPxls.end(); ++it )
circle( res, *it, radius, RED, thickness );
for( it = prBgdPxls.begin(); it != prBgdPxls.end(); ++it )
circle( res, *it, radius, LIGHTBLUE, thickness );
for( it = prFgdPxls.begin(); it != prFgdPxls.end(); ++it )
circle( res, *it, radius, PINK, thickness );
if( rectState == IN_PROCESS || rectState == SET )
rectangle( res, Point( rect.x, rect.y ), Point(rect.x + rect.width, rect.y + rect.height ), GREEN, 2);
imshow( *winName, res );
}
void GCApplication::setRectInMask()
{
CV_Assert( !mask.empty() );
mask.setTo( GC_BGD );
rect.x = max(0, rect.x);
rect.y = max(0, rect.y);
rect.width = min(rect.width, image->cols-rect.x);
rect.height = min(rect.height, image->rows-rect.y);
(mask(rect)).setTo( Scalar(GC_PR_FGD) );
}
void GCApplication::setLblsInMask( int flags, Point p, bool isPr )
{
vector<Point> *bpxls, *fpxls;
uchar bvalue, fvalue;
if( !isPr )
{
bpxls = &bgdPxls;
fpxls = &fgdPxls;
bvalue = GC_BGD;
fvalue = GC_FGD;
}
else
{
bpxls = &prBgdPxls;
fpxls = &prFgdPxls;
bvalue = GC_PR_BGD;
fvalue = GC_PR_FGD;
}
if( flags & BGD_KEY )
{
bpxls->push_back(p);
circle( mask, p, radius, bvalue, thickness );
}
if( flags & FGD_KEY )
{
fpxls->push_back(p);
circle( mask, p, radius, fvalue, thickness );
}
}
void GCApplication::mouseClick( int event, int x, int y, int flags, void* )
{
// TODO add bad args check
switch( event )
{
case EVENT_LBUTTONDOWN: // set rect or GC_BGD(GC_FGD) labels
{
bool isb = (flags & BGD_KEY) != 0,
isf = (flags & FGD_KEY) != 0;
if( rectState == NOT_SET && !isb && !isf )
{
rectState = IN_PROCESS;
rect = Rect( x, y, 1, 1 );
}
if ( (isb || isf) && rectState == SET )
lblsState = IN_PROCESS;
}
break;
case EVENT_RBUTTONDOWN: // set GC_PR_BGD(GC_PR_FGD) labels
{
bool isb = (flags & BGD_KEY) != 0,
isf = (flags & FGD_KEY) != 0;
if ( (isb || isf) && rectState == SET )
prLblsState = IN_PROCESS;
}
break;
case EVENT_LBUTTONUP:
if( rectState == IN_PROCESS )
{
rect = Rect( Point(rect.x, rect.y), Point(x,y) );
rectState = SET;
setRectInMask();
CV_Assert( bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty() );
showImage();
}
if( lblsState == IN_PROCESS )
{
setLblsInMask(flags, Point(x,y), false);
lblsState = SET;
showImage();
}
break;
case EVENT_RBUTTONUP:
if( prLblsState == IN_PROCESS )
{
setLblsInMask(flags, Point(x,y), true);
prLblsState = SET;
showImage();
}
break;
case EVENT_MOUSEMOVE:
if( rectState == IN_PROCESS )
{
rect = Rect( Point(rect.x, rect.y), Point(x,y) );
CV_Assert( bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty() );
showImage();
}
else if( lblsState == IN_PROCESS )
{
setLblsInMask(flags, Point(x,y), false);
showImage();
}
else if( prLblsState == IN_PROCESS )
{
setLblsInMask(flags, Point(x,y), true);
showImage();
}
break;
}
}
int GCApplication::nextIter()
{
if( isInitialized )
grabCut( *image, mask, rect, bgdModel, fgdModel, 1 );
else
{
if( rectState != SET )
return iterCount;
if( lblsState == SET || prLblsState == SET )
grabCut( *image, mask, rect, bgdModel, fgdModel, 1, GC_INIT_WITH_MASK );
else
grabCut( *image, mask, rect, bgdModel, fgdModel, 1, GC_INIT_WITH_RECT );
isInitialized = true;
}
iterCount++;
bgdPxls.clear(); fgdPxls.clear();
prBgdPxls.clear(); prFgdPxls.clear();
return iterCount;
}
GCApplication gcapp;
static void on_mouse( int event, int x, int y, int flags, void* param )
{
gcapp.mouseClick( event, x, y, flags, param );
}
int main( int argc, char** argv )
{
cv::CommandLineParser parser(argc, argv, "{help h||}{#input||}");
if (parser.has("help"))
{
help();
return 0;
}
string filename = parser.get<string>("#input");
if( filename.empty() )
{
cout << "\nDurn, empty filename" << endl;
return 1;
}
Mat image = imread( filename, 1 );
if( image.empty() )
{
cout << "\n Durn, couldn't read image filename " << filename << endl;
return 1;
}
help();
const string winName = "image";
namedWindow( winName, WINDOW_AUTOSIZE );
setMouseCallback( winName, on_mouse, 0 );
gcapp.setImageAndWinName( image, winName );
gcapp.showImage();
for(;;)
{
int c = waitKey(0);
switch( (char) c )
{
case '\x1b':
cout << "Exiting ..." << endl;
goto exit_main;
case 'r':
cout << endl;
gcapp.reset();
gcapp.showImage();
break;
case 'n':
int iterCount = gcapp.getIterCount();
cout << "<" << iterCount << "... ";
int newIterCount = gcapp.nextIter();
if( newIterCount > iterCount )
{
gcapp.showImage();
cout << iterCount << ">" << endl;
}
else
cout << "rect must be determined>" << endl;
break;
}
}
exit_main:
destroyWindow( winName );
return 0;
}
I am new to openCV, I am trying to implement Grabcut. This is the Grabcut example code provided by openCV is samples (same as the one in OpenCV Github repo). However, if you run this code you will get the output in the same window as the input and the output image cannot be saved for post processing.
Consider the code snippet below.
void GCApplication::showImage() const
{
if( image->empty() || winName->empty() )
return;
Mat res;
Mat binMask;
if( !isInitialized )
image->copyTo( res );
else
{
getBinMask( mask, binMask );
image->copyTo( res, binMask );
}
vector<Point>::const_iterator it;
for( it = bgdPxls.begin(); it != bgdPxls.end(); ++it )
circle( res, *it, radius, BLUE, thickness );
for( it = fgdPxls.begin(); it != fgdPxls.end(); ++it )
circle( res, *it, radius, RED, thickness );
for( it = prBgdPxls.begin(); it != prBgdPxls.end(); ++it )
circle( res, *it, radius, LIGHTBLUE, thickness );
for( it = prFgdPxls.begin(); it != prFgdPxls.end(); ++it )
circle( res, *it, radius, PINK, thickness );
if( rectState == IN_PROCESS || rectState == SET )
rectangle( res, Point( rect.x, rect.y ), Point(rect.x + rect.width, rect.y + rect.height ), GREEN, 2);
imshow( *winName, res );
}
I have done the following changes for the function showImage() to return the output image, but it shows an error. How can we use imwrite to save the final output after all iterations, as the function calls are intricate and res keeps on changing. Please help!
cv::Mat& GCApplication::showImage()
{
if( image->empty() || winName->empty() )
return;
Mat res;
Mat binMask;
Mat output;
if( !isInitialized )
image->copyTo( res );
else
{
getBinMask( mask, binMask );
image->copyTo( res, binMask );
res.copyTo(output);
return output;
}
vector<Point>::const_iterator it;
for( it = bgdPxls.begin(); it != bgdPxls.end(); ++it )
circle( res, *it, radius, BLUE, thickness );
for( it = fgdPxls.begin(); it != fgdPxls.end(); ++it )
circle( res, *it, radius, RED, thickness );
for( it = prBgdPxls.begin(); it != prBgdPxls.end(); ++it )
circle( res, *it, radius, LIGHTBLUE, thickness );
for( it = prFgdPxls.begin(); it != prFgdPxls.end(); ++it )
circle( res, *it, radius, PINK, thickness );
if( rectState == IN_PROCESS || rectState == SET )
rectangle( res, Point( rect.x, rect.y ), Point(rect.x + rect.width, rect.y + rect.height ), GREEN, 2);
imshow( *winName, res );
}

You can simply add the following line after the imshow( *winName, res );
cv::imwrite("/Users/path/to/folder/debug.png", res);
Make sure that the path /Users/path/to/folder/ exists.
You were trying to return from a void function, which is not legit, either you can take this shortcut for debugging the image or if you really want to return Mat then you need to change the signature of methods as well.

Related

SDL_GetKeyboardState(NULL) isn't working as expected

I am making a pong clone in C++/SDL2. To take keyboard input from multiple keys, I would like to use SDL_GetKeyboardState(NULL) with a Uint8. However, it isn't working as expected.
Expected behavior: pong paddles move up and down with each keypress.
Actual behavior: keys are not registered.
Here is my code (note that variables written in uppercase letters are from variables.h):
// g++ main.cpp `pkg-config sdl2 SDL2_ttf --cflags --libs`
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <iostream> // For io
#include <time.h> // Rand seeder
const bool debug = true;
// Basic stuff needed for SDL2
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int WINDOW_CENTER_X = WINDOW_WIDTH / 2;
const int WINDOW_CENTER_Y = WINDOW_HEIGHT / 2;
const char* WINDOW_TITLE = "Pong";
// Define the location of the paddles in the bitmap
const int PADDLE_BITMAP_X = 0;
const int PADDLE_BITMAP_Y = 0;
const int BALL_BITMAP_X = 100;
const int BALL_BITMAP_Y = 0;
// Positions of player 1 and player 2
const int PADDLE1_X = 50;
const int PADDLE2_X = WINDOW_WIDTH - 75;
// Paddle width and height
const int PADDLE_WIDTH = 20;
const int PADDLE_HEIGHT = 100;
// Speed of both players
const int PLAYER_SPEED = 0;
// Ball Speed -- this will not be constant
// The Ball Modifier will modify the speed based on the place it lands on the paddle
// Ball speed will never go under 10, and will accelerate over time
// Ball diameter
const int BALL_SPEED = 100;
const int BALL_MODIFIER = 5;
const int BALL_DIAMETER = 10;
const int BALL_CENTER_X = WINDOW_CENTER_X - BALL_DIAMETER;
const int BALL_CENTER_Y = WINDOW_CENTER_Y - BALL_DIAMETER;
const int LANG = 3;
const int LANG_ENGLISH = 3;
const int LANG_GERMAN = 4;
// Game Entity data structure
struct GameObject
{
SDL_Rect ScreenLocation;
SDL_Rect BitmapLocation;
int SpeedX;
int SpeedY;
};
// SDL stuff
SDL_Window* gWindow = NULL;
SDL_Renderer* gWindowRenderer;
SDL_Rect gScreen;
SDL_Event e;
// Game objects
GameObject gPlayer1; // Paddle 1
GameObject gPlayer2; // Paddle 2
GameObject gBall; // Ball
// Scores. I feel like it's better to leave them uninitialized.
int gPlayer1Score;
int gPlayer2Score;
void Render();
bool init();
bool GameQuit;
void Quit();
int main( int argc, char* argv[] )
{
// const Uint8* keyStates = SDL_GetKeyboardState(NULL);
if( init() == false )
{
std::cout << "Init failed. Bye bye!\n";
return 1;
}
while( !GameQuit )
{
const Uint8* keyStates = SDL_GetKeyboardState( NULL );
while( SDL_PollEvent( &e ) )
{
if( e.type == SDL_QUIT )
GameQuit = true;
if( e.type == SDL_KEYDOWN )
{
switch( e.key.keysym.scancode )
{
case SDL_SCANCODE_ESCAPE:
GameQuit = true;
break;
}
}
}
if( keyStates[ SDL_SCANCODE_UP ] )
{
gPlayer1.ScreenLocation.y -= PLAYER_SPEED;
}
if( keyStates[ SDL_SCANCODE_DOWN ] )
{
gPlayer1.ScreenLocation.y += PLAYER_SPEED;
}
if( keyStates[ SDL_SCANCODE_W ] )
{
gPlayer2.ScreenLocation.y -= PLAYER_SPEED;
}
if( keyStates[ SDL_SCANCODE_S ] )
{
gPlayer2.ScreenLocation.y += PLAYER_SPEED;
}
Render();
}
Quit();
return 0;
}
bool init()
{
bool success = true;
if( SDL_Init( SDL_INIT_VIDEO < 0 ) )
{
success = false;
return success;
}
if( TTF_Init() < 0 )
{
success = false;
return success;
}
srand( time( NULL ) ); // Seed rng
// From SDL_Rect.h
// 64 typedef struct SDL_Rect
// 65 {
// 66 int x, y;
// 67 int w, h;
// 68 } SDL_Rect;
GameQuit = false;
gPlayer1.ScreenLocation.y = ( ( WINDOW_HEIGHT / 2 ) - ( PADDLE_HEIGHT / 2 ) );
gPlayer1.ScreenLocation.x = PADDLE1_X;
gPlayer1.ScreenLocation.w = PADDLE_WIDTH;
gPlayer1.ScreenLocation.h = PADDLE_HEIGHT;
gPlayer2.ScreenLocation.y = ( ( WINDOW_HEIGHT / 2 ) - ( PADDLE_HEIGHT / 2 ) );
gPlayer2.ScreenLocation.x = PADDLE2_X;
gPlayer2.ScreenLocation.w = PADDLE_WIDTH;
gPlayer2.ScreenLocation.h = PADDLE_HEIGHT;
gBall.ScreenLocation.x = BALL_CENTER_X;
gBall.ScreenLocation.y = BALL_CENTER_Y;
gBall.ScreenLocation.w = BALL_DIAMETER;
gBall.ScreenLocation.h = BALL_DIAMETER;
gPlayer1.BitmapLocation.x = PADDLE_BITMAP_X;
gPlayer1.BitmapLocation.y = PADDLE_BITMAP_Y;
gPlayer1.BitmapLocation.w = PADDLE_WIDTH;
gPlayer1.BitmapLocation.h = PADDLE_HEIGHT;
gPlayer2.BitmapLocation.x = PADDLE_BITMAP_X;
gPlayer2.BitmapLocation.y = PADDLE_BITMAP_Y;
gPlayer2.BitmapLocation.w = PADDLE_WIDTH;
gPlayer2.BitmapLocation.h = PADDLE_HEIGHT;
gBall.BitmapLocation.x = BALL_BITMAP_X;
gBall.BitmapLocation.y = BALL_BITMAP_Y;
gBall.BitmapLocation.w = BALL_DIAMETER;
gBall.BitmapLocation.h = BALL_DIAMETER;
gPlayer1.SpeedY = PLAYER_SPEED;
gPlayer1.SpeedX = 0;
gPlayer2.SpeedY = PLAYER_SPEED;
gPlayer2.SpeedX = 0;
gPlayer1Score = 0;
gPlayer2Score = 0;
gScreen.x = 0;
gScreen.y = 0;
gScreen.w = WINDOW_WIDTH;
gScreen.h = WINDOW_HEIGHT;
// Note to self: Leave this out since color keying NULL will segfault.
// SDL_SetColorKey(gBitmap, SDL_TRUE, SDL_MapRGB(gBitmap->format, 255, 0, 255));
gWindow = SDL_CreateWindow(
WINDOW_TITLE,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WINDOW_WIDTH,
WINDOW_HEIGHT,
SDL_WINDOW_MAXIMIZED );
// Render SDL Window
gWindowRenderer = SDL_CreateRenderer( gWindow, -1, 0 );
if( gWindowRenderer == nullptr )
{
success = false;
return success;
}
SDL_RenderSetLogicalSize( gWindowRenderer, WINDOW_WIDTH, WINDOW_HEIGHT );
SDL_SetRenderDrawColor( gWindowRenderer, 0x00, 0x00, 0x00, 0xFF );
return success;
}
// Display Menu Text
bool Menu()
{
bool success = false;
if( LANG == LANG_ENGLISH )
{
// DisplayText("Start (G)ame", 350, 250, 12, 255, 255, 255);
// DisplayText("(Q)uit Game", 350, 270, 12, 255, 255, 255);
success = true;
return success;
}
else if( LANG == LANG_GERMAN )
{
// DisplayText("(G) Spielen", 350, 270, 12, 255, 255, 255);
// DisplayText("(Q) Spiel verlassen", 350, 270, 12, 255, 255, 255);
success = true;
return success;
}
else
{
return success;
}
}
void Render()
{
SDL_RenderClear( gWindowRenderer );
SDL_SetRenderDrawColor( gWindowRenderer, 0x00, 0x00, 0x00, 0xFF );
SDL_RenderFillRect( gWindowRenderer, &gScreen );
SDL_SetRenderDrawColor( gWindowRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderFillRect( gWindowRenderer, &gPlayer1.ScreenLocation );
SDL_RenderFillRect( gWindowRenderer, &gPlayer2.ScreenLocation );
SDL_RenderFillRect( gWindowRenderer, &gBall.ScreenLocation );
SDL_RenderPresent( gWindowRenderer );
}
void Quit()
{
SDL_DestroyWindow( gWindow );
SDL_Quit();
}
I'm not sure what I'm doing wrong and would appreciate a concise answer.
Thank you for your time.
Builds & runs fine on my Debian 10 system, though PLAYER_SPEED is zero so nothing useful happens to gPlayer1.ScreenLocation.y/gPlayer2.ScreenLocation.y in the keyStates if-blocks.
Setting PLAYER_SPEED to something other than zero fixes that on my end.

Opencv Dilation and Skeleton(c++)

I have this image :
And I applied dilation with this code :
int dilation_elem = 0;
int dilation_size = 0;
int const max_elem = 2;
int const max_kernel_size = 21;
void Dilation( int, void* );
int main( int argc, char** argv )
{
src = imread("a18.png");
if( !src.data )
{ return -1; }
namedWindow( "Dilation Demo", CV_WINDOW_AUTOSIZE );
cvMoveWindow( "Dilation Demo", src.cols, 0 );
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Dilation Demo",
&dilation_elem, max_elem,
Dilation );
createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo",
&dilation_size, max_kernel_size,
Dilation );
//int dilation_size =7;
/// Default start
Dilation( 0, 0 );
waitKey(0);
return 0;
}
void Dilation( int, void* )
{
int dilation_type;
if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( dilation_type,
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
Point( dilation_size, dilation_size ) );
dilate( src, dilation_dst, element );
imshow( "Dilation Demo", dilation_dst );
imwrite("a18d.png",dilation_dst);
}
And after this step I get this consequent:
And the final step is skeleton :
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
int main()
{
cv::Mat img = cv::imread("a18d.png", 0);
cv::threshold(img, img, 127, 255, cv::THRESH_BINARY);
cv::Mat skel(img.size(), CV_8UC1, cv::Scalar(0));
cv::Mat temp;
cv::Mat eroded;
cv::Mat element = cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3,3));
bool done;
do
{
cv::erode(img, eroded, element);
cv::dilate(eroded, temp, element); // temp = open(img)
cv::subtract(img, temp, temp);
cv::bitwise_or(skel, temp, skel);
eroded.copyTo(img);
done = (cv::countNonZero(img) == 0);
} while (!done);
cv::imshow("Skeleton", skel);
cv::imwrite("18s.png",skel);
cv::waitKey(0);
return 0;
}
code here
And I hve this image :
But I want image like this :
What can I do for this? What is the problem
these steps was done for Image 4 and result is good
Open CV doesn't seem to have a shrink as opposed to an erode operation. At least I can't find it. Try mine
https://github.com/MalcolmMcLean/binaryimagelibrary/blob/master/medialaxistransform.c
Try just using the function "thin" instead of eroding your image.

ROI-based KLT optical tracker in opencv

How can I add roi-based selection in lkdemo.pp( klt optical flow tracker opencv example) source code?
I want select roi in the first frame and track feature point that selected in roi.
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
static void help()
{
// print a welcome message, and the OpenCV version
cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
"Using OpenCV version " << CV_VERSION << endl;
}
Point2f point;
bool addRemovePt = false;
static void onMouse( int event, int x, int y, int , void* )
{
if( event == CV_EVENT_LBUTTONDOWN )
{
point = Point2f((float)x, (float)y);
addRemovePt = true;
}
}
int main( int argc, char** argv )
{
help();
VideoCapture cap(CV_CAP_ANY);
TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
Size subPixWinSize(10,10), winSize(61,61);
const int MAX_COUNT = 500;
bool needToInit = false;
bool nightMode = false;
//if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
//cap.open(argc == 2 ? argv[1][0] - '0' : 0);
//else if( argc == 2 )
//cap.open(argv[1]);
if( !cap.isOpened() )
{
cout << "Could not initialize capturing...\n";
return 0;
}
namedWindow( "LK Demo", 1 );
setMouseCallback( "LK Demo", onMouse, 0 );
Mat gray, prevGray, image;
vector<Point2f> points[2];
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() )
break;
frame.copyTo(image);
cvtColor(image, gray, COLOR_RGB2GRAY);
if( nightMode )
image = Scalar::all(0);
if( needToInit )
{
// automatic initialization
goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
addRemovePt = false;
}
else if( !points[0].empty() )
{
vector<uchar> status;
vector<float> err;
if(prevGray.empty())
gray.copyTo(prevGray);
calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,10, termcrit, 0, 0.001);
size_t i, k;
for( i = k = 0; i < points[1].size(); i++ )
{
if( addRemovePt )
{
if( norm(point - points[1][i]) <= 5 )
{
addRemovePt = false;
continue;
}
}
if( !status[i] )
continue;
points[1][k++] = points[1][i];
circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
}
points[1].resize(k);
}
if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
{
vector<Point2f> tmp;
tmp.push_back(point);
cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
points[1].push_back(tmp[0]);
addRemovePt = false;
}
needToInit = false;
imshow("LK Demo", image);
char c = (char)waitKey(10);
if( c == 27 )
break;
switch( c )
{
case 'r':
needToInit = true;
break;
case 'c':
points[0].clear();
points[1].clear();
break;
case 'n':
nightMode = !nightMode;
break;
}
std::swap(points[1], points[0]);
cv::swap(prevGray, gray);
}
return 0;
}
Here is what I use in those cases :
void SelectNewTemplate(int event, int posx, int posy, int flags, void* userdata)
{
if( event == EVENT_MBUTTONDOWN )
{
waitKey();
}
if( event == CV_EVENT_LBUTTONDOWN )
{
x1pt = posx;
y1pt = posy;
}
if( event == CV_EVENT_LBUTTONUP )
{
x2pt = posx;
y2pt = posy;
Rect newTemp(x1pt, y1pt, (x2pt - x1pt), (y2pt - y1pt));
Mat imgROI = frame(newTemp);
}
}
Usage : pausing the video with middle mouse button, then left clic, drag and let go, press any key to keep going.
After that you can compute your features on your new ROI image : imgROI.
Hope that helps,
Thomas

moving object using slider openCV c++

I'm very new to OpenCV (c++), my lecturer ask me to make a simple slider that every position in slider have different place in the window. My code below can move the object in window based on slider position but when i moved the slider the old position still there. So it looks like a duplicate not move. Can anyone help me on this problem?? Are there a way to solve this problem or I must change the code completely??
#include "stdafx.h"
#include "cv.h"
#include "ml.h"
#include "cxcore.h"
#include "highgui.h"
int g_switch_value = 0;
int colorInt = 0;
void switch_callback( int position ){
if( position == 0 ){
colorInt = 0;
}else{
colorInt = 1;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* name = "Change the color of circle in the picture";
int radius = 30;
int thickness = 12;
int connectivity = 8;
cvscalar red = cv_RGB(0,0,255);
IplImage* src1 = cvLoadImage( "E:/2.jpg" );
CvPoint pt1 = cvPoint(405,195);
CvPoint pt2 = cvPoint(620,400);
cvNamedWindow( name, 1 );
cvShowImage(name, src1);
cvCreateTrackbar( "Change", name, &g_switch_value, 1, switch_callback );
while( 1 ) {
if( colorInt == 0) {
cvCircle(src1,pt1,radius,red,thickness,connectivity);}
else {
cvCircle(src1,pt2,radius,red,thickness,connectivity); }
colorInt == 1;
cvShowImage(name, src1);
if( cvWaitKey( 15 ) == 27 ) break;
}
cvReleaseImage( &src1 );
cvDestroyWindow( name );
return 0; }
Drawing overwrite the image, then you should keep a fresh copy for each different painting.
The easier way:
IplImage* src1 = cvLoadImage( "..." );
IplImage* src2 = cvLoadImage( "..." );
...
while( 1 ) {
if( colorInt == 0) {
cvShowImage(name, src1);
cvCircle(src1,pt1,radius,red,thickness,connectivity);
}
else {
cvShowImage(name, src2);
cvCircle(src2,pt2,radius,red,thickness,connectivity);
}
if( cvWaitKey( 15 ) == 27 ) break;
}
cvReleaseImage( &src1 );
cvReleaseImage( &src2 );

using OpenCV 2.4 loading an image in MFC SDI View or Control

In old version of opencv we had the class like CvvImage, that were easily used for MFC controls to display camera images or simple images from the OpenCV. But in 2.4 or 2.3 this support is finished by the OpenCV , i wonder if we have any other class in new version.
My application is MFC SDI and inside view i am over riding the OnPaint function. In previous opencv i was using Paint function like
void CRightCameraView::OnPaint()
{
CPaintDC dc(this); // device context for painting
m_CVvimageObj.CopyOf(m_iplImageFrame); //copy IplImage frame
if(m_bImageDisplay)
{
m_CVvimageObj.Show(dc.GetSafeHdc(),10,0,m_CVvimageObj.Width(),m_CVvimageObj.Height());
}
}
Kindly guide me which is the most update version of class to do same thing and support such DC controls of MFC.
Since i could not find the answer i would like to help others what i did.
I have extracted cvvimage class manually and added in my project and then i did the following ..
in my views header file i declared
CvvImage m_CVvImageObj;
cv::Mat m_matImage;
inside my CPP file of view , i have called following in initialization function
m_matImage = cv::imread(strA.GetBuffer(),CV_LOAD_IMAGE_COLOR); // Read the file
if(! m_matImage.data ) // Check for invalid input
{
AfxMessageBox(L"Could not open or find the image");
}
IplImage iplimg = m_matImage;
m_CVvImageObj.CopyOf(&iplimg);
UpdateData();
Invalidate();
inside the overided function of OnPaint inside view i added
if(m_CVvImageObj.GetImage())
{
//m_CurrentFrame.Show
m_CVvImageObj.Show(dc.GetSafeHdc(),10,0,m_CVvImageObj.Width(),m_CVvImageObj.Height());
}
Cvvimage h file is the following
#pragma once
//#pragma once
#ifndef CVVIMAGE_CLASS_DEF
#define CVVIMAGE_CLASS_DEF
#include <opencv2\highgui\highgui.hpp>
class CvvImage
{
public:
CvvImage();
virtual ~CvvImage();
virtual bool Create( int width, int height, int bits_per_pixel, int image_origin = 0 );
virtual bool Load( const char* filename, int desired_color = 1 );
virtual bool LoadRect( const char* filename,
int desired_color, CvRect r );
#if defined WIN32 || defined _WIN32
virtual bool LoadRect( const char* filename,int desired_color, RECT r )
{
return LoadRect( filename, desired_color,
cvRect( r.left, r.top, r.right - r.left, r.bottom - r.top ));
}
#endif
virtual bool Save( const char* filename );
virtual void CopyOf( CvvImage& image, int desired_color = -1 );
virtual void CopyOf( IplImage* img, int desired_color = -1 );
IplImage* GetImage() { return m_img; };
virtual void Destroy(void);
int Width() { return !m_img ? 0 : !m_img->roi ? m_img->width : m_img->roi->width; };
int Height() { return !m_img ? 0 : !m_img->roi ? m_img->height : m_img->roi->height;};
int Bpp() { return m_img ? (m_img->depth & 255)*m_img->nChannels : 0; };
virtual void Fill( int color );
virtual void Show( const char* window );
#if defined WIN32 || defined _WIN32
virtual void Show( HDC dc, int x, int y, int width, int height,
int from_x = 0, int from_y = 0 );
virtual void DrawToHDC( HDC hDCDst, RECT* pDstRect );
virtual void DrawToHDC( HDC hDCDst, CvRect pDstRect );
#endif
protected:
IplImage* m_img;
};
typedef CvvImage CImage;
#endif
and CPP file for cvvimage class is following
///////////////////////////////////////////////////////////////////////////////////////////////////
//CvvImage.cpp
#include "StdAfx.h"
#include "CvvImage.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
CV_INLINE RECT NormalizeRect( RECT r );
CV_INLINE RECT NormalizeRect( RECT r )
{
int t;
if( r.left > r.right )
{
t = r.left;
r.left = r.right;
r.right = t;
}
if( r.top > r.bottom )
{
t = r.top;
r.top = r.bottom;
r.bottom = t;
}
return r;
}
CV_INLINE CvRect RectToCvRect( RECT sr );
CV_INLINE CvRect RectToCvRect( RECT sr )
{
sr = NormalizeRect( sr );
return cvRect( sr.left, sr.top, sr.right - sr.left, sr.bottom - sr.top );
}
CV_INLINE RECT CvRectToRect( CvRect sr );
CV_INLINE RECT CvRectToRect( CvRect sr )
{
RECT dr;
dr.left = sr.x;
dr.top = sr.y;
dr.right = sr.x + sr.width;
dr.bottom = sr.y + sr.height;
return dr;
}
CV_INLINE IplROI RectToROI( RECT r );
CV_INLINE IplROI RectToROI( RECT r )
{
IplROI roi;
r = NormalizeRect( r );
roi.xOffset = r.left;
roi.yOffset = r.top;
roi.width = r.right - r.left;
roi.height = r.bottom - r.top;
roi.coi = 0;
return roi;
}
void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin )
{
assert( bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));
BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
memset( bmih, 0, sizeof(*bmih));
bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = width;
bmih->biHeight = origin ? abs(height) : -abs(height);
bmih->biPlanes = 1;
bmih->biBitCount = (unsigned short)bpp;
bmih->biCompression = BI_RGB;
if( bpp == 8 )
{
RGBQUAD* palette = bmi->bmiColors;
int i;
for( i = 0; i < 256; i++ )
{
palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
palette[i].rgbReserved = 0;
}
}
}
CvvImage::CvvImage()
{
m_img = 0;
}
void CvvImage::Destroy()
{
cvReleaseImage( &m_img );
}
CvvImage::~CvvImage()
{
Destroy();
}
bool CvvImage::Create( int w, int h, int bpp, int origin )
{
const unsigned max_img_size = 10000;
if( (bpp != 8 && bpp != 24 && bpp != 32) ||
(unsigned)w >= max_img_size || (unsigned)h >= max_img_size ||
(origin != IPL_ORIGIN_TL && origin != IPL_ORIGIN_BL))
{
assert(0); // most probably, it is a programming error
return false;
}
if( !m_img || Bpp() != bpp || m_img->width != w || m_img->height != h )
{
if( m_img && m_img->nSize == sizeof(IplImage))
Destroy();
m_img = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, bpp/8 );
}
if( m_img )
m_img->origin = origin == 0 ? IPL_ORIGIN_TL : IPL_ORIGIN_BL;
return m_img != 0;
}
void CvvImage::CopyOf( CvvImage& image, int desired_color )
{
IplImage* img = image.GetImage();
if( img )
{
CopyOf( img, desired_color );
}
}
#define HG_IS_IMAGE(img) \
((img) != 0 && ((const IplImage*)(img))->nSize == sizeof(IplImage) && \
((IplImage*)img)->imageData != 0)
void CvvImage::CopyOf( IplImage* img, int desired_color )
{
if( HG_IS_IMAGE(img) )
{
int color = desired_color;
CvSize size = cvGetSize( img );
if( color < 0 )
color = img->nChannels > 1;
if( Create( size.width, size.height,
(!color ? 1 : img->nChannels > 1 ? img->nChannels : 3)*8,
img->origin ))
{
cvConvertImage( img, m_img, 0 );
}
}
}
bool CvvImage::Load( const char* filename, int desired_color )
{
IplImage* img = cvLoadImage( filename, desired_color );
if( !img )
return false;
CopyOf( img, desired_color );
cvReleaseImage( &img );
return true;
}
bool CvvImage::LoadRect( const char* filename,
int desired_color, CvRect r )
{
if( r.width < 0 || r.height < 0 ) return false;
IplImage* img = cvLoadImage( filename, desired_color );
if( !img )
return false;
if( r.width == 0 || r.height == 0 )
{
r.width = img->width;
r.height = img->height;
r.x = r.y = 0;
}
if( r.x > img->width || r.y > img->height ||
r.x + r.width < 0 || r.y + r.height < 0 )
{
cvReleaseImage( &img );
return false;
}
if( r.x < 0 )
{
r.width += r.x;
r.x = 0;
}
if( r.y < 0 )
{
r.height += r.y;
r.y = 0;
}
if( r.x + r.width > img->width )
r.width = img->width - r.x;
if( r.y + r.height > img->height )
r.height = img->height - r.y;
cvSetImageROI( img, r );
CopyOf( img, desired_color );
cvReleaseImage( &img );
return true;
}
bool CvvImage::Save( const char* filename )
{
if( !m_img )
return false;
cvSaveImage( filename, m_img );
return true;
}
void CvvImage::Show( const char* window )
{
if( m_img )
cvShowImage( window, m_img );
}
void CvvImage::Show( HDC dc, int x, int y, int w, int h, int from_x, int from_y )
{
if( m_img && m_img->depth == IPL_DEPTH_8U )
{
uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
BITMAPINFO* bmi = (BITMAPINFO*)buffer;
int bmp_w = m_img->width, bmp_h = m_img->height;
FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );
from_x = MIN( MAX( from_x, 0 ), bmp_w - 1 );
from_y = MIN( MAX( from_y, 0 ), bmp_h - 1 );
int sw = MAX( MIN( bmp_w - from_x, w ), 0 );
int sh = MAX( MIN( bmp_h - from_y, h ), 0 );
SetDIBitsToDevice(
dc, x, y, sw, sh, from_x, from_y, from_y, sh,
m_img->imageData + from_y*m_img->widthStep,
bmi, DIB_RGB_COLORS );
}
}
void CvvImage::DrawToHDC( HDC hDCDst, RECT* pDstRect )
{
if( pDstRect && m_img && m_img->depth == IPL_DEPTH_8U && m_img->imageData )
{
uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
BITMAPINFO* bmi = (BITMAPINFO*)buffer;
int bmp_w = m_img->width, bmp_h = m_img->height;
CvRect roi = cvGetImageROI( m_img );
CvRect dst = RectToCvRect( *pDstRect );
if( roi.width == dst.width && roi.height == dst.height )
{
Show( hDCDst, dst.x, dst.y, dst.width, dst.height, roi.x, roi.y );
return;
}
if( roi.width > dst.width )
{
SetStretchBltMode(
hDCDst, // handle to device context
HALFTONE );
}
else
{
SetStretchBltMode(
hDCDst, // handle to device context
COLORONCOLOR );
}
FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );
::StretchDIBits(hDCDst,dst.x, dst.y, dst.width, dst.height,roi.x, roi.y, roi.width, roi.height,\
m_img->imageData, bmi, DIB_RGB_COLORS, SRCCOPY );
}
}
void CvvImage::DrawToHDC( HDC hDCDst, CvRect pDstRect )
{
RECT rDest=CvRectToRect(pDstRect);
DrawToHDC(hDCDst,&rDest);
}
void CvvImage::Fill( int color )
{
cvSet( m_img, cvScalar(color&255,(color>>8)&255,(color>>16)&255,(color>>24)&255) );
}
enjoy.
1.first load a picture get pic name and path.
void CvlistDlg::OnBnClickedRead()
{
CString FilePathName;
CString FileName;
CFileDialog openVideo(true);
openVideo.m_ofn.lpstrTitle = _T("Open");
if (IDOK == openVideo.DoModal()){
FileName = openVideo.GetFileName();
FilePathName = openVideo.GetPathName();
_currentFile._fileName = FileName;
_currentFile._filePath = FilePathName;
}
}
2.add follow code in your button callback function.
void CvlistDlg::OnBnClickedOk(){
cv::Mat img = cv::imread(str);
CDC *pDC = GetDlgItem(IDC_Pic)->GetDC();
HDC hDC = pDC->GetSafeHdc();
CRect rect;
GetDlgItem(IDC_Pic)->GetClientRect(&rect);
int rw = rect.right - rect.left; //the width of your picture control
int rh = rect.bottom - rect.top;
if (!img.data){
MessageBox(_T("read picture fail!"));
return;
}
cv::resize(img, img, cv::Size(rw, rh));
IplImage iplimg = img;
m_CVvImageObj.CopyOf(&iplimg);
UpdateData();
if (m_CVvImageObj.GetImage()){
m_CVvImageObj.Show(pDC->GetSafeHdc(), 0, 0, rw, rh);
}
}
I hope those can give you some help.
You don't need of IplImage. Basically you can draw a cv::Mat to a DC of a MFC control, using StretchDIBits.
It accepts pointer to memory block where the bitmap is stored... in this case cv::Mat.data
But remember that (this make me crazing before)
GDI requires DWORD alignment while cv::Mat don't use any rows padding
In addition your Mat could be a ROI this means that at the end each rows there isn't next row but the image that continues
You can have DWORD alignment in OpenCV using images where cols % 4 == 0 so you can add a border to your image
border = 4 - (img.cols % 4);
if ( (border<4 && border > 0) || img.isContinuous() == false)
{
cv::copyMakeBorder(img, tmpImg, 0, 0, 0, border, cv::BORDER_CONSTANT, 0);
}
else
tmpImg = img
Using copyMakeBorder you will solve both requirements
Another issues is grey images because MFC requires a flat palette to display it. You can convert your Mat with cvtColor or create the palette:
uchar buffer[sizeof(BITMAPINFO) + 1024];
BITMAPINFO* bmi = (BITMAPINFO*)buffer;
BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
bmih->biBitCount = 8*img.elemenSize()
bmih->biWidth = tmpImg.cols;
bmih->biHeight = -tmpImg.rows;// DIB are bottom ->top
bmih->...
//The palette is required for grey image
if (bmih->biBitCount == 8)
{
RGBQUAD* palette = bmi->bmiColors;
for (int i = 0; i < 256; i++)
{
palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
palette[i].rgbReserved = 0;
}
}
finally draw to HDC
imgH = img.rows;
imgW = img.cols; //use original cols to avoid border
ClientDC hDC(WinCtrl);
//rr is relative to control
RECT rr; WinCtrl->GetClientRect(&rr);
StretchDIBits(hDC,
0, 0, rr.right, rr.bottom,
0, 0, imgW, imgH,
tmpImg.data, bmi, DIB_RGB_COLORS, SRCCOPY);
You can find detailed info and a class to manage image fitting here:
http://www.pklab.net/?&id=390&lang=EN&t=How-to-display-an-OpenCV-image-or-video-in-your-own-MFC-interface