示例#1
0
文件: BasicPro.cpp 项目: Tjuly/ME
QVector<colorNum> BasicPro::Maincolor(QImage qImg)
{
    cv::Mat mat = cv::Mat(qImg.height(), qImg.width(), CV_8UC4, (uchar*)qImg.bits(), qImg.bytesPerLine());
    cv::Mat Img = cv::Mat(mat.rows, mat.cols, CV_8UC3 );
    int from_to[] = { 0,0, 1,1, 2,2 };
    cv::mixChannels( &mat, 1, &Img, 1, from_to, 3 );

    const int K = 7;

    cv::Mat ImgCvt;
    cvtColor(Img,ImgCvt,CV_BGR2Lab);

    cv::Mat ImgData(Img.rows*Img.cols,1,CV_32FC3);
    cv::Mat_<cv::Vec3b>::iterator itCvt=ImgCvt.begin<cv::Vec3b>();
    cv::Mat_<cv::Vec3f>::iterator itData=ImgData.begin<cv::Vec3f>();
    for(;itCvt<ImgCvt.end<cv::Vec3b>();itCvt += 2,itData += 2)
        *itData=*itCvt;

    cv::Mat ImgLab,ImgCen;
    kmeans(ImgData,K,ImgLab,cv::TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,10,1.0),1,cv::KMEANS_PP_CENTERS,ImgCen);

    cv::Mat ImgCenBGR(ImgCen.rows,1,CV_8UC3);
    for(int i=0;i<ImgCen.rows;++i)
    {
        float *pCen=ImgCen.ptr<float>(i);
        ImgCenBGR.at<cv::Vec3b>(i,0) = cv::Vec3b(uchar(pCen[0]),uchar(pCen[1]),uchar(pCen[2]));
    }

    cvtColor(ImgCenBGR,ImgCenBGR,CV_Lab2BGR);

    QVector<cv::Vec3b> vecCen(ImgCen.rows);
    for(int i=0;i<ImgCen.rows;++i)
        vecCen[i]=ImgCenBGR.at<cv::Vec3b>(i,0);

    cv::Mat ImgRes(Img.size(),CV_8UC3);
    QVector<colorNum> vecColorNum(ImgCen.rows);
    for(size_t i=0;i<vecColorNum.size();++i)
    {
        vecColorNum[i].color=vecCen[i];
        vecColorNum[i].num=0.0;
    }

    cv::Mat_<int>::iterator itLabel = ImgLab.begin<int>();
    cv::Mat_<cv::Vec3b>::iterator itRes = ImgRes.begin<cv::Vec3b>();
    for(;itLabel<ImgLab.end<int>();++itLabel,++itRes)
    {
        *itRes = vecCen[*itLabel];
        vecColorNum[*itLabel].num += 1.0;
    }

    std::sort(vecColorNum.begin(), vecColorNum.end(), cmpColorNum);
    for(size_t i=0; i<vecColorNum.size(); ++i)
        vecColorNum[i].num /= double(ImgLab.rows);

    return vecColorNum;
}
示例#2
0
AnnotatedDatasetInfo getAnnotatedImageData(Mat Image, string datafile){
	AnnotatedDatasetInfo info;	
	int rows = 240;
	int cols = 320;
	float depth = 50.00;

	Mat_<signed short int> Labels(rows, cols, CV_16SC1);
	vector< vector<Point> > Points(1);
	Mat_<Vec3f> Depths(rows, cols, CV_32FC3);

	// datafile is binary image that represent object mask
	// 1 represent object, 0 means non object
	ifstream ImgData(datafile.c_str());
	int row = 0;

	if (ImgData.is_open()){
		while (ImgData.good())	{
			string line;
			getline(ImgData, line);
			if(line.length() == 0) continue;

			int col = 0;
			for(unsigned int i = 0 ; i < line.length() ; i++){
				char ch = line[i];

				// add data into "Labels"
				if(ch != ' '){
					if(ch == '1') {
						Labels(row, col) = 0;
						// add Point (row, col) in points for class 2
						Point p(col, row);
						Points[0].push_back(p);
					}
					else { 
						Labels(row, col) = -3;
						// add Point (row, col) in points for class 1
						//Point p(col, row);
						//Points[0].push_back(p);
					}
					// add depth into "Depths"
					// Depths is a R X C X 3 matrix where each pixel has (X,Y,Z) location in world coords
					Depths(row, col) = Vec3f(float(col), float(row), depth); 
					col++;
				}
			}
			row++;
		}
		ImgData.close();
	}
	else cout << "Unable to open file"; 
	info.Labels = Labels;
	info.Points = Points;
	info.Depths = Depths;
	return info;
}