Trying to implement Lego Java core code in C++

asked 2018-02-06 15:16:34 -0500

Marko5280 gravatar image

updated 2018-02-06 15:22:35 -0500

I am in the process of implementing the Lego LeJos core in C++. I have the OpenCV samples "Contours" and "drawContours" running. I am using them to convert the Lego software from Java to C++. I get all the way to "drawContours". At that point I am stuck. "imshow" hits an error. The program stops at ...

int idx = 0;                                     //  From "drawContours.cpp"
    for (; idx >= 0; idx = hierarchy[idx][0]) {      //  From "drawContours.cpp"
        Countt += 1;
        cout << Countt;
        printf("\n");

        //idx = hierarchy[idx][0];
        drawContours(
            dst,         //  Image
            contours,    //  Contours
            idx,         //  Contouridx
            color,       //  Color
            1,           //  Thickness, Defalt = 1
            LINE_8,      //  LineType, Defalt = LINE_8,
            hierarchy);  //  Hierarchy =
    }
    imshow("After drawContours", dst);

The entire program is ...

#include "stdafx.h"
#include <iostream>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"

using namespace std;
using namespace cv;

/// Global Variables
int DELAY_CAPTION = 1500;
int DELAY_BLUR = 100;
int MAX_KERNEL_LENGTH = 31;

Mat src; Mat dst;
Mat frame;
char window_name[] = "Smoothing Demo";
int bias = 0;
int Countt = 0;

/// Function headers
int display_caption(const char* caption);
int display_dst(int delay);

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

/**
* function main

  */
  int main(int argc, char ** argv)
    {
Mat frame;
frame = imread("lena.jpg");
printf("lena.jpg\n");
imshow("Lena.jpg", frame);
waitKey();
//Mat roi = new Mat(frame, new Rect(10, 2 * frame.rows() / 3, frame.cols() - 20, camImage.rows() / 12));
//Mat roi = Mat (frame, new Rect(10, 10, 10, 10));
Mat mono;
printf("cvtColor\n");
cvtColor(frame, mono, COLOR_BGR2GRAY);
imshow("cvtColor", mono);
waitKey();
Mat Iblur;
printf("GaussianBlur\n");
GaussianBlur(mono, Iblur, Size(9, 9), 2, 2);
imshow("GaussionBlur", Iblur);
waitKey();
Mat thresh;
printf("threshold\n");
threshold(Iblur, thresh, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
imshow("threshold", thresh);
waitKey();
Mat erodeImg;
printf("Before erode\n");
erode(thresh, erodeImg, 1);
imshow("After erode", erodeImg);
waitKey();
Mat dilateImg;
printf("Before dilate\n");
dilate(erodeImg, dilateImg, 1);
imshow("After dilate", dilateImg);
waitKey();
//List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
printf("Before findcontours\n");

findContours(dilateImg, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);

imshow("After findContours", dilateImg);
waitKey();
vector<vector<Point> > contours;
vector<Point>  cont;
cout << Countt;
printf("\n");
Scalar color(rand() & 255, rand() & 255, rand() & 255);
int idx = 0;                                     //  From "drawContours.cpp"
for (; idx >= 0; idx = hierarchy[idx][0]) {      //  From "drawContours.cpp"
    Countt += 1;
    cout << Countt;
    printf("\n");
    drawContours(
        dst,         //  Image
        contours,    //  Contours
        idx,         //  Contouridx
        color,       //  Color
        1,           //  Thickness, Defalt = 1
        LINE_8,      //  LineType, Defalt = LINE_8,
        hierarchy);  //  Hierarchy =
}
imshow("After drawContours", dst);
waitKey();
edit retag flag offensive close merge delete