Beispiel #1
0
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
    try
    {
        Mat3b image  = cv_bridge::toCvShare(msg, "bgr8")->image;
        Mat3b result = yaed.Onvideotracking(image);
        loc=yaed.locations();
        //ROS_INFO("location: %d,%d",loc.xaxis,loc.yaxis);

        //geometry_msgs::Point point;
        detecter::CircleTarget target;
        target.tar.x=loc.xaxis;
        target.tar.y=loc.yaxis;
        target.image_height = image.size().height;
        target.image_width  = image.size().width;
        if(target.tar.x!=0&&target.tar.y!=0)
            ppb->publish(target);


        sensor_msgs::ImagePtr imagemsg;
        imagemsg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", result).toImageMsg();
        ppub->publish(imagemsg);

    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
    }
}
Beispiel #2
0
float Greenness::greenness(Mat im){
    Mat rgbim = im.clone();
    Mat3b image;
    
    cvtColor(rgbim,image,CV_BGR2HSV);
    
    int aveH = 0;
    int aveS = 0;
    int aveV = 0;
    int pixelct = 0;
    
    for (Mat3b::iterator it = image.begin(); it != image.end(); it++) {
        Vec3b hsv = *it;
        int H=hsv.val[0];
        int S=hsv.val[1];
        int V=hsv.val[2];
        
        if((H != 0 && S != 0 && V != 255)){
            pixelct++;
            aveH+=H;
            aveS+=S;
            aveV+=V;
        } else {
        }
    }
    
    float lccval = compareLCCValues(aveH/pixelct,aveS/pixelct,aveV/pixelct);
    lccval = floor(lccval * 1000.0) / 1000.0;
    
    
    cvtColor(image, image, CV_HSV2BGR);
    
    char name[30];
    sprintf(name,"Average H=%d",aveH/pixelct);
    putText(image,name, Point(10,20) , FONT_HERSHEY_SIMPLEX, .5, Scalar(0,0,0), 2,8,false );
    
    sprintf(name,"Average S=%d",aveS/pixelct);
    putText(image,name, Point(10,50) , FONT_HERSHEY_SIMPLEX, .5, Scalar(0,0,0), 2,8,false );
    
    sprintf(name,"Average V=%d",aveV/pixelct);
    putText(image,name, Point(10,80) , FONT_HERSHEY_SIMPLEX, .5, Scalar(0,0,0), 2,8,false );
    
    sprintf(name,"Pixels=%d",pixelct);
    putText(image,name, Point(10,110) , FONT_HERSHEY_SIMPLEX, .5, Scalar(0,0,0), 2,8,false );
    
    sprintf(name,"LCC Value: %.1f", lccval);
    putText(image,name, Point(10,140) , FONT_HERSHEY_SIMPLEX, .5, Scalar(0,0,0), 2,8,false );
    
    rectangle(image, Point(10,150), Point(30,170), HSVtoRGBcvScalar(aveH/pixelct, aveS/pixelct, aveV/pixelct),CV_FILLED);
    
    result = image;
    
    return lccval;
    
}
Beispiel #3
0
int main(int argc, char **argv)
{
	if (argc != 3)
	{
		fprintf(stdout, "usage: %s inputImageName threshNum\n", argv[0]);
		return -1;
	}
	string name(argv[1]);
	int thresh = atoi(argv[2]);

	Mat3b src = cvLoadImage(argv[1]);
	show_mat(src);

	Mat3b frame = src.clone();
	floodFill(frame, Point(1,1),Scalar(0,0,0), NULL, Scalar::all(thresh), Scalar::all(thresh));
	show_mat(frame);

	Mat1b gray;
	vGrayScale(frame, gray);

	// Create mat with alpha channel
	cv::Mat4b dst(src.size());

	for (int j = 0; j < dst.rows; ++j) 
	{
		for (int i = 0; i < dst.cols; ++i)
		{
			cv::Vec4b& rgba = dst(j, i);
			cv::Vec3b& rgb = src(j, i); 
			rgba[0] = rgb[0];
			rgba[1] = rgb[1];
			rgba[2] = rgb[2];
			if (gray(j,i) > 0)
				rgba[3] = 255;
			else
				rgba[3] = 0;
		}
	}

	try {
		std::vector<int> params;
		params.push_back(CV_IMWRITE_PNG_COMPRESSION);
		params.push_back(9);
		cvSaveImage(string(name+".png").c_str(), &(IplImage)dst, &params[0]);
	}
	catch (std::runtime_error& ex) {
		fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
		return 1;
	}

	fprintf(stdout, "Saved PNG file with alpha data.\n");

	waitKey();
}
void lane_analysis_handler(carmen_bumblebee_basic_stereoimage_message * stereo_image) {

	// if there is no car pose yet, return
	if (car_pose == nullptr) {
		printf("I do not see any car pose at the moment... sorry!\n");
		return;
	}

	// get the image from the bumblebee
	Mat3b image;
	if (stereo_image->image_size == 3686400) image = Mat3b(960, 1280);
	else image = Mat3b(480, 640);

	if (camera_side == CameraSide::LEFT) image.data = (uchar *) stereo_image->raw_left;
	else if(camera_side == CameraSide::RIGHT) image.data = (uchar *) stereo_image->raw_right;
	else image.data = (uchar *) stereo_image->raw_right;

	cvtColor(image, image, CV_RGB2BGR);
	cv::resize(image, image, Size(640,480));

	fnumber++;

//	if(fnumber>100)
//	{
//		exit(0);
//	}

	if (!image.empty()) {
		cout << "frame: " << fnumber << endl;
		// run ELAS
		ELAS::run(image);
		printf("CARMEN::ELAS... done!\n");

		// get the raw message
		printf("get_raw_message()... ");
		_raw_elas_message = ELAS::get_raw_message();
		_raw_elas_message.idx_frame = fnumber;
		printf("done!\n");

		// publish messages
		lane_analysis_publish_messages(stereo_image->timestamp);

#ifdef SHOW_DISPLAY
		// display viz
		ELAS::display(image, &_raw_elas_message);
#endif

	} else {
		printf("End of dataset!\n");
	}
}
Beispiel #5
0
void KalmanHoughs::view(HoughDoMeio *houghDoMeio, const Mat &colorFramePerspectiva, const Mat3b &colorFrameRoiIPM, const Scalar &cor) {
	// display perspectiva
	Mat imgPerspectiva = colorFramePerspectiva.clone();
	if (houghDoMeio != NULL) houghDoMeio->draw(imgPerspectiva, cor, config);

	// ipm
	Mat3b imgIPM = colorFrameRoiIPM.clone();
	Rect ipm = Rect(0, 0, colorFrameRoiIPM.cols, colorFrameRoiIPM.rows);
	if (houghDoMeio != NULL) {
		HoughLine _hough = HoughLine::create(*houghDoMeio, config);
		houghDoMeio->draw(imgIPM, cor);
	}
	imgIPM.copyTo(imgPerspectiva(ipm));
	imshow("KalmanHoughs", imgPerspectiva);
}
Beispiel #6
0
vpx_image Camera::getLastVPXImage()
{
    Mat3b frame = getLastFrame();
    vpx_image img;
    int w = frame.size().width, h = frame.size().height;
    vpx_img_alloc(&img, VPX_IMG_FMT_I420, w, h, 1); // I420 == YUV420P, same as YV12 with U and V switched

    size_t i=0, j=0;
    for( int line = 0; line < h; ++line )
    {
        const cv::Vec3b *srcrow = frame[line];
        if( !(line % 2) )
        {
            for( int x = 0; x < w; x += 2 )
            {
                uint8_t r = srcrow[x][2];
                uint8_t g = srcrow[x][1];
                uint8_t b = srcrow[x][0];

                img.planes[VPX_PLANE_Y][i] = ((66*r + 129*g + 25*b) >> 8) + 16;
                img.planes[VPX_PLANE_V][j] = ((-38*r + -74*g + 112*b) >> 8) + 128;
                img.planes[VPX_PLANE_U][j] = ((112*r + -94*g + -18*b) >> 8) + 128;
                i++;
                j++;

                r = srcrow[x+1][2];
                g = srcrow[x+1][1];
                b = srcrow[x+1][0];
                img.planes[VPX_PLANE_Y][i] = ((66*r + 129*g + 25*b) >> 8) + 16;
                i++;
            }
        }
        else
        {
            for( int x = 0; x < w; x += 1 )
            {
                uint8_t r = srcrow[x][2];
                uint8_t g = srcrow[x][1];
                uint8_t b = srcrow[x][0];

                img.planes[VPX_PLANE_Y][i] = ((66*r + 129*g + 25*b) >> 8) + 16;
                i++;
            }
        }
    }
void ELAS::viz_lane_measurement_generation(const Mat3b & _colorFrame, HoughLine &esq, HoughLine &dir) {
	Mat3b viz_image = _colorFrame.clone();

	// visualizar as houghs finais
	Scalar preto = Scalar(0, 0, 0);
	if (!esq.isEmpty()) esq.draw(viz_image, esqCor);
	if (!dir.isEmpty()) dir.draw(viz_image, dirCor);
	if (!esq.isEmpty()) esq.draw(viz_image, preto, 1);
	if (!dir.isEmpty()) dir.draw(viz_image, preto, 1);

	imshow("lane_measurement_generation", viz_image);
	waitKey();
}
Beispiel #8
0
Mat1b Nieto::filtro(const Mat3b &inColorFrame, int tauInicio, int tauFim, int thres) {
	double tempoInicio = static_cast<double>(getTickCount());

	Mat1b outBinaryFrameFiltrado = Mat1b(inColorFrame.size());
	// converte para cinza
	cvtColor(inColorFrame, outBinaryFrameFiltrado, CV_BGR2GRAY);
	// aplica o filtro do Nieto
	nietoLaneMarkingsDetector(outBinaryFrameFiltrado, outBinaryFrameFiltrado, tauInicio, tauFim);
	// aplica o threshold na imagem filtrada
	threshold(outBinaryFrameFiltrado, outBinaryFrameFiltrado, thres, 255, CV_THRESH_BINARY);

	// calcula o tempo de execu��o
	double tempoFim = static_cast<double>(getTickCount());
	double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000;

	// exibe as sa�das definidas (texto e/ou imagem)
	if (verbose) cout << "- nieto.filtro: " << tempoExecutando << " ms" << endl;

	return outBinaryFrameFiltrado;
}
int main(int argc, char** argv)
{
   if(argc != 2){
	cout<<"Provide input image";
        return 0;
   }

    // Read image
    Mat3b img = imread(argv[1]);

    // Binarize image. Text is white, background is black
    Mat1b bin;
    cvtColor(img, bin, COLOR_BGR2GRAY);
    bin = bin < 200;

    // Rotate the image according to the found angle
    Mat1b rotated;
    bin.copyTo(rotated);

   // Mat M = getRotationMatrix2D(box.center, box.angle, 1.0);    //warpAffine(bin, rotated, M, bin.size());

    // Compute horizontal projections
    Mat1f horProj;
    reduce(rotated, horProj, 1, CV_REDUCE_AVG);

    // Remove noise in histogram. White bins identify space lines, black bins identify text lines
    float th = 0;
    Mat1b hist = horProj <= th;

    // Get mean coordinate of white pixels groups
    vector<int> ycoords;
    int y = 0;
    int count = 0;
    bool isSpace = false;
    for (int i = 0; i < rotated.rows; ++i)
    {
        if (!isSpace)
        {
            if (hist(i))
            {
                isSpace = true;
                count = 1;
                y = i;
            }
        }
        else
        {
            if (!hist(i))
            {
                isSpace = false;
                ycoords.push_back(y / count);
            }
            else
            {
                y += i;
                count++;
            }
        }
    }
    // Draw line as final result
    Mat3b result;
    cvtColor(rotated, result, COLOR_GRAY2BGR);

if(ycoords.size()>0){
    for (int i = 0; i < ycoords.size()-1; i++)
    {
	Rect rect1;
	rect1.x = 0;
	rect1.y = ycoords[i];
	rect1.width = result.size().width;
	rect1.height = ycoords[i+1]-ycoords[i];
        if(rect1.height > 30){
		Mat Image1 = result(rect1);
		imshow("Display Image", Image1); 
		string name = "";
		std::stringstream ss; 
		ss << i;
		name = "Image"+ss.str()+".jpg";
		imwrite(name,Image1);
		waitKey(0);
        }
	if(i == ycoords.size()-2){
		Rect rect1;
		rect1.x = 0;
		rect1.y = ycoords[i+1];
		rect1.width = result.size().width;
		rect1.height = result.size().height-ycoords[i+1];
		if(rect1.height > 30){
			Mat Image1 = result(rect1);
			imshow("Display Image", Image1); 
			string name = "";
			std::stringstream ss; 
			ss << i+1;
			name = "Image"+ss.str()+".jpg";
			imwrite(name,Image1);
			waitKey(0);
		}	
	}
    }
}
else
   cout<<"No coordinates formed";
   return 0;
}
Beispiel #10
0
Mat visionUtils::skinDetect(Mat captureframe, Mat3b *skinDetectHSV, Mat *skinMask, std::vector<int> adaptiveHSV, int minPixelSize, int imgBlurPixels, int imgMorphPixels, int singleRegionChoice, bool displayFaces)
{

    if (adaptiveHSV.size()!=6 || adaptiveHSV.empty())
    {
        adaptiveHSV.clear();
        adaptiveHSV.push_back(5);
        adaptiveHSV.push_back(38);
        adaptiveHSV.push_back(51);
        adaptiveHSV.push_back(17);
        adaptiveHSV.push_back(250);
        adaptiveHSV.push_back(242);
    }


    //int step = 0;
    Mat3b frameTemp;
    Mat3b frame;
    // Forcing resize to 640x480 -> all thresholds / pixel filters configured for this size.....
    // Note returned to original size at end...
    Size s = captureframe.size();
    cv::resize(captureframe,captureframe,Size(640,480));



    if (useGPU)
    {
        GpuMat imgGPU, imgGPUHSV;
        imgGPU.upload(captureframe);
        cv::cvtColor(imgGPU, imgGPUHSV, CV_BGR2HSV);
        GaussianBlur(imgGPUHSV, imgGPUHSV, Size(imgBlurPixels,imgBlurPixels), 1, 1);
        imgGPUHSV.download(frameTemp);
    }
    else
    {
        cv::cvtColor(captureframe, frameTemp, CV_BGR2HSV);
        GaussianBlur(frameTemp, frameTemp, Size(imgBlurPixels,imgBlurPixels), 1, 1);
    }

    // Potential FASTER VERSION using inRange
    Mat frameThreshold = Mat::zeros(frameTemp.rows,frameTemp.cols, CV_8UC1);
    Mat hsvMin = (Mat_<int>(1,3) << adaptiveHSV[0], adaptiveHSV[1],adaptiveHSV[2] );
    Mat hsvMax = (Mat_<int>(1,3) << adaptiveHSV[3], adaptiveHSV[4],adaptiveHSV[5] );
    inRange(frameTemp,hsvMin ,hsvMax, frameThreshold);
    frameTemp.copyTo(frame,frameThreshold);

    /* BGR CONVERSION AND THRESHOLD */
    Mat1b frame_gray;

    // send HSV to skinDetectHSV for return
    *skinDetectHSV=frame.clone();

    cv::cvtColor(frame, frame_gray, CV_BGR2GRAY);


    // Adaptive thresholding technique
    // 1. Threshold data to find main areas of skin
    adaptiveThreshold(frame_gray,frame_gray,255,ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY_INV,9,1);


    if (useGPU)
    {
        GpuMat imgGPU;
        imgGPU.upload(frame_gray);
        // 2. Fill in thresholded areas
#if CV_MAJOR_VERSION == 2
        gpu::morphologyEx(imgGPU, imgGPU, CV_MOP_CLOSE, Mat1b(imgMorphPixels,imgMorphPixels,1), Point(-1, -1), 2);
        gpu::GaussianBlur(imgGPU, imgGPU, Size(imgBlurPixels,imgBlurPixels), 1, 1);
#elif CV_MAJOR_VERSION == 3
        //TODO: Check if that's correct
        Mat element = getStructuringElement(MORPH_RECT, Size(imgMorphPixels, imgMorphPixels), Point(-1, -1));
        Ptr<cuda::Filter> closeFilter = cuda::createMorphologyFilter(MORPH_CLOSE, imgGPU.type(), element, Point(-1, -1), 2);
        closeFilter->apply(imgGPU, imgGPU);
        cv::Ptr<cv::cuda::Filter> gaussianFilter = cv::cuda::createGaussianFilter(imgGPU.type(), imgGPU.type(), Size(imgMorphPixels, imgMorphPixels), 1, 1);
        gaussianFilter->apply(imgGPU, imgGPU);
#endif

        imgGPU.download(frame_gray);
    }
    else
    {
        // 2. Fill in thresholded areas
        morphologyEx(frame_gray, frame_gray, CV_MOP_CLOSE, Mat1b(imgMorphPixels,imgMorphPixels,1), Point(-1, -1), 2);
        GaussianBlur(frame_gray, frame_gray, Size(imgBlurPixels,imgBlurPixels), 1, 1);
        // Select single largest region from image, if singleRegionChoice is selected (1)
    }


    if (singleRegionChoice)
    {
        *skinMask = cannySegmentation(frame_gray, -1, displayFaces);
    }
    else // Detect each separate block and remove blobs smaller than a few pixels
    {
        *skinMask = cannySegmentation(frame_gray, minPixelSize, displayFaces);
    }

    // Just return skin
    Mat frame_skin;
    captureframe.copyTo(frame_skin,*skinMask);  // Copy captureframe data to frame_skin, using mask from frame_ttt
    // Resize image to original before return
    cv::resize(frame_skin,frame_skin,s);

    if (displayFaces)
    {
        imshow("Skin HSV (B)",frame);
        imshow("Adaptive_threshold (D1)",frame_gray);
        imshow("Skin segmented",frame_skin);
    }

    return frame_skin;
    waitKey(1);
}