Ask Your Question
0

use create Trackbar for Threshold's value

asked 2017-06-28 11:44:36 -0600

louis89 gravatar image

updated 2017-06-29 03:46:02 -0600

pklab gravatar image

I have a Threshold function that For Various Images Minimum amount of Threshold Changes Between 0.00001 and 0.001 and the maximum amount of threshold is 255. I want to use Trackbar for specify minimum amount of Threshold.can you help me to do this?thanks a lot...

int main(int argc, char* argv[]){
VideoCapture cap(0); 
if (!cap.isOpened()){
    cout << "Cannot open the video cam" << endl;
    return -1;}
int totalFrameNumber = cap.get(CV_CAP_PROP_FRAME_COUNT);
Mat frame;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE);
while (1)
{  
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess)
{
    cout << "Cannot read a frame from video stream" << endl;
    break;
}
Rect rectangle2(420,280, 40, 40);
rectangle(frame, rectangle2, Scalar(255, 255, 255));
Mat cornerstrength;
cornerHarris(frame, cornerstrength, 3, 3, 0.1);
//threshold the corner strength
Mat harriscorners;
double th = 0.00001;
threshold(cornerstrength, harriscorners, th, 255, THRESH_BINARY);
morphologyEx(harriscorners, harriscorners, MORPH_CLOSE, Mat(), Point(-1, -1), 6);
//local maxima detection
Mat dilated, localMax;
dilate(cornerstrength, dilated, Mat());
compare(cornerstrength, dilated, localMax, CMP_EQ);
threshold(cornerstrength, harriscorners, th, 255, THRESH_BINARY);
harriscorners.convertTo(harriscorners, CV_8U);
bitwise_and(harriscorners, localMax, harriscorners);
harriscorners.convertTo(harriscorners, CV_32F);
Mat S(0, 2, CV_32SC1);

//drawing a circle around corners

for (int j = 0;j < harriscorners.rows;j++)
    for (int i = 0;i < harriscorners.cols;i++)
    {

        if (harriscorners.at<float>(j, i)> 0)
        {    circle(frame, Point(i, j), 5, Scalar(255), 2, 8);
            Mat pt(1, 2, CV_32SC1);
            pt.at<int>(1, 0) = i;
            pt.at<int>(0, 1) = j;
            // Add the point to S
            S.push_back(pt);
            for (int x = 430; x < 460; x++)
                for (int y = 285; y < 315; y++)
                    if  ((pt.at<int>(1, 0) = i) == x && (pt.at<int>(0, 1) = j) == y))
                    {
                       cout<<"louis89"<<endl;}}}
imshow("MyVideo", frame);
if (waitKey(30) == 27)
            {
                cout << "esc key is pressed by user" << endl;
                break;
            }
        }

        return 0;
    }
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-06-28 13:17:06 -0600

pklab gravatar image

updated 2017-06-29 03:55:22 -0600

simple proportion ? you might use 0..100% as int range than scale it to your float range.

You can also choose the resolution value using a code like below

struct  myData {
    double thMin = 0.00001;
    double thMax = 0.001;
    double resolution = thMin / 2.0;
    double Range(){ return thMax - thMin; }
    int Ticks() { return cvRound(Range() / resolution); };
    int tbValue = cvRound(Ticks() / 2.0); // start at 50%
    double Value(){
        return thMin + tbValue * Range() / Ticks();
    }
};
void onTrackBar(int, void* userData)
{
    myData* pdata = (myData*)userData;
    double thFloat = pdata->Value();
    //std::cout << cv::format("threshold: %0.5f", thFloat) << endl;
    std::cout << "threshold:" << thFloat << endl;
}
void main()
{
    myData data;
    static const char * winName = "MyWindow";
    cv::namedWindow(winName);
    cv::createTrackbar("Threshold", winName, &(data.tbValue), data.Ticks(), onTrackBar, &data);
    onTrackBar(0, &data);
    cv::waitKey(1); // press a key on window
   ... put your code here ...
}

if you like 0..100% use int Ticks() { return 100; };

UPDATES

  • I can't see any trackbar in your code... use mine in double th = data.Value();// 0.00001;
  • your code can't works because cornerHarris requires Input single-channel 8-bit or floating-point image doc

THREAD UN-SAFE

Using trackbar to change a shared var fails in thread safety. Production grade application must use locks or other concurrency control.

other tips

Use std::vector<Point> S instead of Mat S(0, 2, CV_32SC1); and simply S.push_back(Point(i, j))

To checks whether a rectangle contains a point use Rec::contains:

Rect checkRect(Point(430, 285), Point(285, 315));
if (checkRect.contains(Point(i, j)))
    cout << "louis89" << endl;
edit flag offensive delete link more

Comments

Thanks for you'r help pklab.I'm a beginner in opencv.when I use your code in my program,I encounter an error.if I write my code,can you help my to do this?

louis89 gravatar imagelouis89 ( 2017-06-28 16:16:11 -0600 )edit

threshold(cornerstrength, harriscorners,th , 255, THRESH_BINARY); th is minimum amount of threshold that I want to use trackbar for it.

louis89 gravatar imagelouis89 ( 2017-06-28 16:37:20 -0600 )edit

@louis89 My code runs without error on my OpenCV installation. I encounter an error isn't right way to get help. Please check updated answer.

pklab gravatar imagepklab ( 2017-06-29 03:44:42 -0600 )edit

thanks very much pklab.but when I run you'r code in yhis line: void onTrackBar(int, void* userData) { myData* pdata = (myData*)userData; double thFloat = pdata->Value(); //std::cout << cv::format("threshold: %0.5f", thFloat) << endl; std::cout << "threshold:" << thFloat << endl; }** I encounter and error that say error expected a';' why this happen and what can I do to solve this?I put your code after this line in my code :writer.open("test.avi", -1, 20, frame.size(), 0);**

louis89 gravatar imagelouis89 ( 2017-06-29 08:21:26 -0600 )edit

I've suspect you are using void onTrackBar(int... in your main ? my advice is to go back to learn c++. BTW you have to put your code where I had written ... put your code here ...

pklab gravatar imagepklab ( 2017-06-29 09:16:01 -0600 )edit

you are welcome :)

pklab gravatar imagepklab ( 2017-06-30 09:26:08 -0600 )edit
0

answered 2017-06-29 09:39:18 -0600

louis89 gravatar image

Thanks pklab.My problem has been resolved

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2017-06-28 11:44:36 -0600

Seen: 1,293 times

Last updated: Jun 29 '17