コード例 #1
0
ファイル: image.cpp プロジェクト: 93sam/opencv
bool CvMatrix::load( const char* filename, const char* matname, int color )
{
    CvMat* m = 0;
    if( icvIsXmlOrYaml(filename) )
    {
        m = icvRetrieveMatrix(cvLoad(filename,0,matname));

        if( (CV_MAT_CN(m->type) > 1) != (color == 0) )
            CV_Error( CV_StsNotImplemented,
            "RGB<->Grayscale conversion is not implemented for matrices stored in XML/YAML" );
        /*{
            CvMat* temp_mat;
            temp_mat = cvCreateMat( m->rows, m->cols,
                CV_MAKETYPE(CV_MAT_DEPTH(m->type), color > 0 ? 3 : 1 )));
            cvCvtColor( m, temp_mat, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
            cvReleaseMat( &m );
            m = temp_mat;
        }*/
    }
#ifdef HAVE_OPENCV_HIGHGUI
    else
        m = cvLoadImageM( filename, color );
#endif

    set( m, false );
    return m != 0;
}
コード例 #2
0
void TestRGB()
{
	const char *pszImg1 = "E:\\对比照片\\图片分组\\图片分组\\不同日期同一时间拍摄\\1天\\20120814-20120815-0939\\01\\1.jpg";

	CvMat * Mat = cvLoadImageM(pszImg1,1);

	/*double dValue = cvmGet(Mat,100,100);
	printf("像素值为%f\n",dValue);*/
	cvNamedWindow("1",CV_WINDOW_AUTOSIZE);
	cvShowImage("1",Mat);

	cvWaitKey(0);
}
コード例 #3
0
ファイル: Machine.cpp プロジェクト: jeremt/emotionDetector
void Machine::train() {

  // Get data from json.
  CvSize sz = cvGetSize(cvLoadImage(_imgPatterns.begin()->second[0].c_str()));
  _imgSize = sz.width * sz.height;
  _nbImgs = 0;
  for (auto const &pattern : _imgPatterns)
    _nbImgs += pattern.second.size();

  // Initialize cv materials.
  cv::Mat sum = cv::Mat::zeros(_imgSize, 1, CV_32FC1);
  cv::Mat matrix(_imgSize, _nbImgs, CV_32FC1);
  std::size_t offset = 0;

  // Compute sum and matrix from images data.
  for (auto const &pattern : _imgPatterns) {
    for (std::string const &path : pattern.second) {
      LOG_INFO << "[" << offset << "] [" << pattern.first << "] "
                << path << LOG_ENDL;
      cv::Mat m = cvLoadImageM(path.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
      m = m.t();
      m = m.reshape(1, _imgSize);
      m.convertTo(m, CV_32FC1);
      m.copyTo(matrix.col(offset));
      sum = sum + m;
      ++offset;
    }
  }

  // Compute eigen vectors from the images sum.
  cv::Mat mean = sum / float(_nbImgs);
  cv::Mat crossMatrix = cv::Mat(_imgSize, _nbImgs, CV_32FC1);
  for (std::size_t i = 0; i < _nbImgs; ++i)
    crossMatrix.col(i) = matrix.col(i) - mean;
  cv::Mat cMatrix = crossMatrix.t() * crossMatrix;
  cv::Mat vMatrix, lMatrix;
  eigen(cMatrix, lMatrix, vMatrix);

  // Compute projection matrix.
  cv::Mat uMatrix = crossMatrix * vMatrix;
  _projectionMatrix = uMatrix.t();

  // project the training set to the faces space
  _trainSet = _projectionMatrix * crossMatrix;

}
コード例 #4
0
ファイル: cards.c プロジェクト: Hoevers/CardDetector
/**
 * Initialise the card template engine
 * <directory>/cards/<card_type_string>.png
 * @param directory The directory where to search the templates
 */
int
card_init(const char *directory)
{
	enum card_type current  = CARD_JOKER;
	CvMat *current_image = NULL;
	char filename[256] = { 0 };
	FILE *file = NULL;

	/* Process every card template */
	for(current = CARD_JOKER; current < CARD_TYPE_END; current++)
	{
		snprintf(filename, sizeof(filename), "%s/cards/%s.png", directory, card_type_string[current]);
		printf("%s, ", filename);

		file = fopen(filename, "r");
		if( file == NULL )
		{
			printf("doesn't exist!\n");
			fclose(file);
			break;
			//exit(-1);
		}
		else
			printf("file exist :-)!\n");

		fclose(file);

		current_image = cvLoadImageM( filename, CV_LOAD_IMAGE_GRAYSCALE );
		if( current_image != NULL )
		{
			card_process( current_image, current );
			cvReleaseMat( &current_image );
		}
	}


	return 0;
}
コード例 #5
0
int main(int argc, char **argv){
	gpio_init();
    CvMat *matA = cvLoadImageM(argv[1],0);
    if(!matA) return(-1);
    else printf("%s is %dx%d\n",argv[1],matA->cols,matA->rows);
	int i, j;
	imageblink:
    for (j=0; j<matA->rows; j++){
		if ((j==0) & (i==0)){
			gpio_blink(0xc);
			gpio_blink(0xc); // just to be safe
			}
		else{
			gpio_blink(0xd);
			gpio_blink(0xd); // just to be safe
			}
		for (i=0; i<matA->cols; i++){	
			temp= matA->data.ptr[j*matA->cols+i];
			if ((temp==0xc) || (temp==0xd)) gpio_blink(0xe);
			else gpio_blink(temp);
			}	
		}
	goto imageblink;	
    }
コード例 #6
0
ファイル: main.c プロジェクト: mafraba/weedo
int main(int argc, char** argv)
{
    // Load and display original image
    puts("Loading image...");
    CvMat* img = cvLoadImageM(PATH, CV_LOAD_IMAGE_COLOR);
    CvMat* orig = cvCloneMat(img);
    cvCvtColor(img, img, CV_BGR2Lab);
    if (SMOOTH_ORIGINAL)
    {
        cvSmooth(img, img, CV_GAUSSIAN, SMOOTH_ORIGINAL, 0, 0, 0);
    }
    
    //chromacity(img);

    //show(ORIGINAL_IMAGE_WINDOW_NAME, orig);
    //show(PRETREATED_IMAGE_WINDOW_NAME, img);

    // Generate a Gabor filter bank
    puts("Generating Gabor filter bank...");
    FilterBank filter_bank;
    generate_gabor_filter_bank(&filter_bank,
                               N_BANDWIDTHS, bandwidths,
                               N_FREQS, spatial_frequencies,
                               N_ORIENTATIONS, orientations);

    // Separate each channel
    puts("Separating channels...");
    CvMat *ch1 = cvCreateMat(img->rows, img->cols, CV_8UC1);
    CvMat *ch2 = cvCreateMat(img->rows, img->cols, CV_8UC1);
    CvMat *ch3 = cvCreateMat(img->rows, img->cols, CV_8UC1);
    cvSplit(img, ch1, ch2, ch3, NULL);

    // Apply the filter bank on each one of them
    puts("Applying filters...");
    CvMat **results = (CvMat**) malloc(3 * filter_bank.size * sizeof (CvMat*));
    CvMat **filtered_channel_1 = results;
    apply_filter_bank(&filter_bank, ch1, filtered_channel_1);
    CvMat **filtered_channel_2 = results + filter_bank.size;
    apply_filter_bank(&filter_bank, ch2, filtered_channel_2);
    CvMat **filtered_channel_3 = results + 2 * filter_bank.size;
    apply_filter_bank(&filter_bank, ch3, filtered_channel_3);

    // Now sort the samples
    puts("Sorting...");
    int n_channels = (IGNORAR_L ? 2 : 3);
    results = (IGNORAR_L ? filtered_channel_2 : results);
    CvMat *samples;
    sort_samples(n_channels * filter_bank.size, results, &samples);
    printf("Samples: %d(x%d)", samples->rows, samples->cols);
    fflush(stdout);

    // And cluster them
    printf("Clustering... ");
    CvScalar color_tab[8];
    color_tab[0] = CV_RGB(255, 0, 0);
    color_tab[1] = CV_RGB(0, 255, 0);
    color_tab[2] = CV_RGB(0, 0, 255);
    color_tab[3] = CV_RGB(0, 255, 255);
    color_tab[4] = CV_RGB(255, 0, 255);
    color_tab[5] = CV_RGB(255, 255, 0);
    color_tab[6] = CV_RGB(255, 255, 255);
    color_tab[7] = CV_RGB(0, 0, 0);

    CvMat *labels = cvCreateMat(samples->rows, 1, CV_32SC1);
    cvKMeans2(samples, K_CLUSTERS, labels,
              cvTermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0),
              10, NULL, 0, NULL, NULL);
    puts("done");
    fflush(stdout);

    CvMat *color_labels = cvCreateMat(img->rows, img->cols, CV_8UC3);
    CvMat **classes = malloc(K_CLUSTERS * sizeof (CvMat*));
    for (int i = 0; i < K_CLUSTERS; i++)
    {
        classes[i] = cvCreateMat(img->rows, img->cols, CV_8UC1);
        cvZero(classes[i]);
    }
    img_from_labels(labels, classes, color_labels, color_tab);
    //show("Labels", labeled_img);

    CvMat *mix = cvClone(img);
    cvAddWeighted(orig, 0.7, color_labels, 0.3, 0, mix);

    //
    puts("Outputting...");
    char out_file_name[256];
    sprintf(out_file_name, "%s/%s.png", OUTPUT_PATH, "original");
    cvSaveImage(out_file_name, orig, NULL);
    output_base_channels(img);
    if (!IGNORAR_L)
    {
        output_filtered_images("CH1", filter_bank.size, filtered_channel_1);
    }
    output_filtered_images("CH2", filter_bank.size, filtered_channel_2);
    output_filtered_images("CH3", filter_bank.size, filtered_channel_3);
    output_filter_bank(&filter_bank);
    // output labels
    output_classes(classes, orig);
    // output colored and mix
    sprintf(out_file_name, "%s/%s.png", OUTPUT_PATH, "coloured");
    cvSaveImage(out_file_name, color_labels, NULL);
    sprintf(out_file_name, "%s/%s.png", OUTPUT_PATH, "mix");
    cvSaveImage(out_file_name, mix, NULL);

    //show("Mix", mix);
    //    cvWaitKey(0);
    //    cvWaitKey(0);
    //    cvWaitKey(0);
    // Should do some cleanup here... :_(

    return (EXIT_SUCCESS);
}
コード例 #7
0
ファイル: main.cpp プロジェクト: wcs7846/MarkLHF
int main()
{
//	IplImage* img = cvLoadImage("PictureSource1.jpg");
//	IplImage* temp = 0;
//	/*---------------------------------------------------------------------------------------------------*/
//	cvNamedWindow("Testing");
//	temp = dopyrdownIpl(img);
//	cvShowImage("Testing",temp);
	/*---------------------------------------------------------------------------------------------------*/
	CvMat* mattesting = cvLoadImageM("PictureSource1.jpg");;
	CvMat* matpyr = 0;
	matpyr = dopydownMat(mattesting);
	cvNamedWindow("MatPYR", CV_WINDOW_AUTOSIZE);
	cvShowImage("MatPYR", matpyr);
	/*---------------------------------------------------------------------------------------------------*/
#if TEST==RECTANGLE
	CvMat* Mattemp = 0;
	Mattemp = cvCloneMat(matpyr);
	cvRectangle(Mattemp, cvPoint(5, 10), cvPoint(255, 255), cvScalar(255, 255, 255));
	cvNamedWindow("MatClone", CV_WINDOW_AUTOSIZE);
	cvShowImage("MatClone", Mattemp);
	cvWaitKey(0);
	cvDestroyWindow("MatClone");
	cvReleaseMat(&Mattemp);
#endif
	/*---------------------------------------------------------------------------------------------------*/
#if TEST==CANNY
	CvMat* MatCannyIn;
	CvMat* MatCannyOut = cvCreateMat(matpyr->rows, matpyr->cols, matpyr->type);
	MatCannyIn = cvCloneMat(matpyr);
	MatCannyOut = docannyMat(MatCannyIn, 10, 100);

	cvNamedWindow("MatCanny");
	cvShowImage("MatCanny",MatCannyOut);
	cvWaitKey(0);
	cvDestroyWindow("MatCanny");
	cvReleaseMat(&MatCannyIn);
	cvReleaseMat(&MatCannyOut);
#endif
	/*---------------------------------------------------------------------------------------------------*/ 
#if TEST==MAT_ELEM
	CvMat* MatELEM = cvCreateMat(matpyr->rows, matpyr->cols, matpyr->type);
	float element_test = CV_MAT_ELEM(*MatELEM, float, matpyr->rows-2, matpyr->rows-3);
	printf("element_test = %f\r\n", element_test);
	cvWaitKey(0);
	cvNamedWindow("MatELEM");
	cvShowImage("MatELEM", MatELEM);
	cvWaitKey(0);
 	cvDestroyWindow("MatELEM");
#endif
	/*---------------------------------- -----------------------------------------------------------------*/
#if TEST==GETMATPointData
	float val[] = { 0.86,-0.5,0.5,0.84,0, 0.86,-0.5,0.5,0.84,0, 0.86,-0.5,0.5,0.84,0, 0.86,-0.5,0.5,0.84,0, 0.86,-0.5,0.5,0.84,0 };
	float* Point = 0;
	float SUMResult = 0;
	CvMat* MatSUM = cvCreateMat(5, 3, CV_32FC1);
	cvInitMatHeader(MatSUM,MatSUM->rows,MatSUM->cols,MatSUM->type,&val);
	for (size_t row = 0; row < MatSUM->rows; row++)
	{
		Point = (float*)(MatSUM->data.ptr + row*MatSUM->step);
		for (size_t col = 0; col < MatSUM->cols; col++)
		{
			printf("%f\t",*Point); 
			SUMResult += *(Point);
			Point++;
		}
		printf("\r\n");
	}
	printf("TheSUMResult=%f\r\n",SUMResult);
	printf("MatSUM->rows=%d\r\n", MatSUM->rows);
	printf("MatSUM->cols=%d\r\n ", MatSUM->cols);
	
	cvWaitKey(0);
	cvReleaseMat(&MatSUM);
#endif
	/*---------------------------------- -----------------------------------------------------------------*/
#if TEST==ROITEST
	/*
		Using the ROI to come true 
	*/
	IplImage* MatToImage = cvCreateImage(cvGetSize(matpyr), IPL_DEPTH_8U, 3);
	cvGetImage(matpyr, MatToImage);
	cvNamedWindow("MatToImage");
	cvShowImage("MatToImage", MatToImage);
	cvWaitKey(0);

	cvSetImageROI(MatToImage, cvRect(10, 10, matpyr->rows - 30, matpyr->cols - 60));
	cvAddS(MatToImage, cvScalar(200), MatToImage);
	cvResetImageROI(MatToImage);
	
	cvNamedWindow("ROITEST");
	cvShowImage("ROITEST", MatToImage);
	cvWaitKey(0);
	cvDestroyWindow("ROITEST");
#endif
	/*---------------------------------- -----------------------------------------------------------------*/
#if TEST==WIDTHSTEPTEST
	/*
		Using WidthStep to come true
	*/
	//The picture source is matpyr
	
#endif
	/*---------------------------------- -----------------------------------------------------------------*/
	printf("\tEND\t \r\n");
	cvWaitKey(0);
//	cvReleaseImage(&img); 
//	cvReleaseImage(&temp);
	cvReleaseMat(&matpyr);
	cvReleaseMat(&mattesting);
	cvDestroyAllWindows();
}
コード例 #8
0
void imagedemo_002()
{ 
	/* 002 - 图像的读取、显示和写入 */
	std::string imgPathStr = projectPath + "images\\opencv-logo2.png";
	cv::Mat img_COLOR_Mat = cv::imread(imgPathStr, CV_LOAD_IMAGE_COLOR);
	cv::Mat img_GRAYSCALE_Mat = cv::imread(imgPathStr, CV_LOAD_IMAGE_GRAYSCALE);
	const char* fileNameCharArr = imgPathStr.c_str();
	IplImage* img_COLOR_IplImage = cvLoadImage(fileNameCharArr, \
		CV_LOAD_IMAGE_COLOR);
	IplImage* img_GRAYSCALE_IplImage = cvLoadImage(fileNameCharArr, \
		CV_LOAD_IMAGE_GRAYSCALE);
	CvMat* img_COLOR_CvMat = cvLoadImageM(fileNameCharArr, \
		CV_LOAD_IMAGE_COLOR);
	CvMat* img_GRAYSCALE_CvMat = cvLoadImageM(fileNameCharArr, \
		CV_LOAD_IMAGE_GRAYSCALE);

	std::string title_COLOR_Mat = "cv::Mat COLOR";
	std::string title_GRAYSCALE_Mat = "cv::Mat GRAYSCALE";
	std::string title_COLOR_CvMat = "CvMat COLOR";
	std::string title_GRAYSCALE_CvMat = "CvMat GRAYSCALE";
	std::string title_COLOR_IplImage = "IplImage COLOR";
	std::string title_GRAYSCALE_IplImage = "IplImage GRAYSCALE";
	const char* char_title_COLOR_CvMat = title_COLOR_CvMat.c_str();
	const char* char_title_GRAYSCALE_CvMat = title_GRAYSCALE_CvMat.c_str();
	const char* char_title_COLOR_IplImage = title_COLOR_IplImage.c_str();
	const char* char_title_GRAYSCALE_IplImage = title_COLOR_IplImage.c_str();
	cv::imshow(title_COLOR_Mat, img_COLOR_Mat); 
	cv::waitKey();	
	cv::imshow(title_GRAYSCALE_Mat, img_GRAYSCALE_Mat);
	cv::waitKey();
	cvShowImage(char_title_COLOR_CvMat, img_COLOR_CvMat);
	cv::waitKey();
	cvShowImage(char_title_GRAYSCALE_CvMat, img_GRAYSCALE_CvMat);
	cv::waitKey();
	cvShowImage(char_title_COLOR_IplImage, img_COLOR_IplImage);
	cv::waitKey();
	cvShowImage(char_title_GRAYSCALE_IplImage, img_GRAYSCALE_IplImage);
	cv::waitKey();

	/*Release Memory*/
	cvReleaseImage(&img_COLOR_IplImage);
	cvReleaseImage(&img_GRAYSCALE_IplImage);
	cvReleaseMat(&img_COLOR_CvMat);
	cvReleaseMat(&img_GRAYSCALE_CvMat);

	/*Check if the image is readed correctly*/
	if (!img_COLOR_Mat.data) {
		std::cout<<"img_COLOR_Mat is not created."<<std::endl;
	} else {
		std::cout<<"img_COLOR_Mat is readed correctly."<<std::endl;
	}
	std::string wrongImgPathStr = projectPath + "images\\wrong.png";
	cv::Mat wrongImg = cv::imread(wrongImgPathStr,-1);
	if (!wrongImg.data) {
		std::cout<<"wrongImage is not created."<<std::endl;
	} else {
		std::cout<<"wrongImage is readed correctly."<<std::endl;
	}

	/*Write image*/
	std::string imwriteMatPath = projectPath + "outputs\\imwriteDemo_Mat.jpg";
	bool flag_Mat = cv::imwrite(imwriteMatPath, img_GRAYSCALE_Mat);
	std::cout<<"flag_Mat = "<<flag_Mat<<std::endl;

	std::string tmp_CvMatPath = projectPath + "outputs\\imwriteDemo_CvMat.jpg";
	const char* imwriteCvMatPath = tmp_CvMatPath.c_str();
	//int flag_CvMat = cvSaveImage(imwriteCvMatPath, img_GRAYSCALE_CvMat);
	//std::cout<<"flag_CvMat = "<<flag_CvMat<<std::endl;

	std::string tmp_IplImagePath = projectPath + "outputs\\imwriteDemo_IplImage.jpg";
	const char* imwriteIplImagePath = tmp_IplImagePath.c_str();
	//int flag_IplImage = cvSaveImage(imwriteIplImagePath, img_GRAYSCALE_IplImage);
	//std::cout<<"flag_IplImage = "<<flag_IplImage<<std::endl;

}