Пример #1
0
const Mat& CmCurveEx::CalFirDer(int kSize, float linkEndBound, float linkStartBound)
{
	Mat dxMat, dyMat;
	Sobel(m_img1f, dxMat, CV_32F, 1, 0, kSize);
	Sobel(m_img1f, dyMat, CV_32F, 0, 1, kSize);

	for (int y = 0; y < m_h; y++)
	{
		float *dx = dxMat.ptr<float>(y);
		float *dy = dyMat.ptr<float>(y);
		float *pOrnt = m_pOrnt1f.ptr<float>(y);
		float *pDer = m_pDer1f.ptr<float>(y);
		for (int x = 0; x < m_w; x++)
		{
			pOrnt[x] = (float)atan2f(dx[x], -dy[x]);
			if (pOrnt[x] < 0.0f)
				pOrnt[x] += PI2;

			pDer[x] = sqrt(dx[x]*dx[x] + dy[x]*dy[x]);
		}
	}

	GaussianBlur(m_pDer1f, m_pDer1f, Size(3, 3), 0);
	normalize(m_pDer1f, m_pDer1f, 0, 1, NORM_MINMAX);
	NoneMaximalSuppress(linkEndBound, linkStartBound);
	return m_pDer1f;
}
void BagOfWordsSlic::MoveCentroidsToLocalGradientMinima() {

    Mat grayscale_input_image(input_image_.rows, input_image_.cols, input_image_.depth());
    cvtColor(input_image_, grayscale_input_image, COLOR_BGR2GRAY);

    Mat horizontal_gradient_image(grayscale_input_image.size(),CV_32F);
    Mat vertical_gradient_image(grayscale_input_image.size(),CV_32F);
    Mat gradient_magnitude_image(grayscale_input_image.size(),CV_32F);

    Sobel(grayscale_input_image,horizontal_gradient_image,gradient_magnitude_image.depth(),1,0);
    Sobel(grayscale_input_image,vertical_gradient_image,gradient_magnitude_image.depth(),0,1);
    gradient_magnitude_image = horizontal_gradient_image.mul(horizontal_gradient_image) + vertical_gradient_image.mul(vertical_gradient_image);

    int x_ind, y_ind;
    float min_magnitude;
    Point adjusted_centroid, neighboring_point;

    for(int i = 0; i < cluster_centroids_.size(); ++i) {
        min_magnitude = FLT_MAX;
        x_ind = min(max((int)cluster_centroids_[i].pt_.x,1),im_width_-2);
        y_ind = min(max((int)cluster_centroids_[i].pt_.y,1),im_height_-2);
        adjusted_centroid = Point(x_ind, y_ind);
        for(int j = 0; j < 8; ++j) {
            neighboring_point = adjusted_centroid + eight_neighbors[j];
            if(gradient_magnitude_image.at<float>(neighboring_point) < min_magnitude) {
                cluster_centroids_[i].pt_ = neighboring_point;
                min_magnitude = gradient_magnitude_image.at<float>(neighboring_point);
            }
        }
    }
}
Пример #3
0
void AntiShake::sobelOperator(Mat &src, Mat &output, int scale, int delta) {
	int ddepth = CV_16S;

	GaussianBlur(src, output, Size(3, 3), 0, 0, BORDER_DEFAULT);
//	blur( src1, output1, Size( blurSize, blurSize), Point(point, point) );

	if (output.type() == 16) {
		/// Convert it to gray
		cvtColor(output, output, CV_RGB2GRAY);
	}
	/// Generate grad_x and grad_y
	Mat grad_x, grad_y, grad;
	Mat abs_grad_x, abs_grad_y;

	int border = BORDER_ISOLATED;
	/// Gradient X
	//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
	Sobel(output, grad_x, ddepth, 1, 0, 3, scale, delta, border);
	convertScaleAbs(grad_x, abs_grad_x);

	/// Gradient Y
	//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
	Sobel(output, grad_y, ddepth, 0, 1, 3, scale, delta, border);
	convertScaleAbs(grad_y, abs_grad_y);

	/// Total Gradient (approximate)
	addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
	grad.copyTo(output);

	grad_x.release();
	grad_y.release();
	abs_grad_x.release();
	abs_grad_y.release();
}
Пример #4
0
QVector<Data*> FSobel::run(QVector<Data*> dat)
{
    DImage1* d = dynamic_cast<DImage1*>(dat[0]);

    Mat sMat;

    switch (type)
    {
        case 0:
            Sobel(d->getImage(), sMat,-1,1,0,ksize);
            break;
        default:
            Sobel(d->getImage(), sMat, -1, 0, 1, ksize);
            break;
    }

    DImage1* a = new DImage1();
    a->setImage(sMat);

    QVector<Data*> ans;
    ans.push_back(a);

    return ans;

}
Пример #5
0
void CurvatureCentral2(IplImage *nx, IplImage *ny, IplImage* dst)
{
    if (!nx || !ny || !dst)
    {
        return;
    }

    CvSize size = cvSize(nx->width, nx->height);
    IplImage* nxx = cvCreateImage(size,IPL_DEPTH_32F,1);
    IplImage* nyy = cvCreateImage(size,IPL_DEPTH_32F,1);
    IplImage* junk = cvCreateImage(size,IPL_DEPTH_32F,1);

    Sobel(nx,nxx,junk);
    Sobel(ny,junk,nyy);

    CvScalar cx,cy,crt;
    for (int i=0; i<size.height; i++)
    {
        for (int j=0; j<size.width; j++)
        {
            cx = cvGet2D(nxx,i,j);
            cy = cvGet2D(nyy,i,j);
            crt.val[0] = cx.val[0] + cy.val[0];
            cvSet2D(dst,i,j,crt);
        }
    }

    cvReleaseImage(&nxx);
    cvReleaseImage(&nyy);
    cvReleaseImage(&junk);
}
Пример #6
0
bool ObjPatchMatcher::ComputeDominantLine(const Mat& mask_patch, Point tl_pt, Point3f& line_coeff, int& obj_pt_sign) {
	Mat grad_x, grad_y, grad_mag;
	Sobel(mask_patch, grad_x, CV_32F, 1, 0);
	Sobel(mask_patch, grad_y, CV_32F, 0, 1);
	magnitude(grad_x, grad_y, grad_mag);

	float mean_grad_x = mean(grad_x).val[0];
	float mean_grad_y = mean(grad_y).val[0];
	// line: y=kx+b; k=-meanx/meany + pass center
	Point centerp(tl_pt.x+mask_patch.cols/2, tl_pt.y+mask_patch.rows/2);
	float k = -mean_grad_x / mean_grad_y;
	float b = centerp.y - k*centerp.x;
	line_coeff.x = k;
	line_coeff.y = -1;
	line_coeff.z = b;

	float obj_pos = 0;
	Point tl(centerp.x-mask_patch.cols/2, centerp.y-mask_patch.rows/2);
	for(int r=0; r<mask_patch.rows; r++) for(int c=0; c<mask_patch.cols; c++) {
		obj_pos += mask_patch.at<float>(r,c)*(line_coeff.x*(tl.x+c)+line_coeff.y*(tl.y+r)+line_coeff.z>0? 1:-1);
	}
	obj_pt_sign = obj_pos>0? 1: -1;

	return true;
}
Пример #7
0
CMat& CmCurveEx::CalSecDer(CMat &img1f, int kSize)
{
	AllocSpace(img1f.size());
	Mat dxx, dxy, dyy;
	Sobel(img1f, dxx, CV_32F, 2, 0, kSize);
	Sobel(img1f, dxy, CV_32F, 1, 1, kSize);
	Sobel(img1f, dyy, CV_32F, 0, 2, kSize);

	double eigval[2], eigvec[2][2];
	for (int y = 0; y < _h; y++){
		float *xx = dxx.ptr<float>(y);
		float *xy = dxy.ptr<float>(y);
		float *yy = dyy.ptr<float>(y);
		float *pOrnt = _pOrnt1f.ptr<float>(y);
		float *pDer = _pDer1f.ptr<float>(y);
		for (int x = 0; x < _w; x++){
			compute_eigenvals(yy[x], xy[x], xx[x], eigval, eigvec);
			pOrnt[x] = (float)atan2(-eigvec[0][1], eigvec[0][0]); //计算法线方向
			if (pOrnt[x] < 0.0f)
				pOrnt[x] += PI2;
			pDer[x] = float(eigval[0] > 0.0f ? eigval[0] : 0.0f);//计算二阶导数
		}
	}
	GaussianBlur(_pDer1f, _pDer1f, Size(3, 3), 0);
	return _pDer1f;
}
Пример #8
0
const Mat& CmCurveEx::CalSecDer(int kSize, float linkEndBound, float linkStartBound)
{
	Mat dxx, dxy, dyy;
	Sobel(m_img1f, dxx, CV_32F, 2, 0, kSize);
	Sobel(m_img1f, dxy, CV_32F, 1, 1, kSize);
	Sobel(m_img1f, dyy, CV_32F, 0, 2, kSize);

	double eigval[2], eigvec[2][2];
	for (int y = 0; y < m_h; y++)
	{
		float *xx = dxx.ptr<float>(y);
		float *xy = dxy.ptr<float>(y);
		float *yy = dyy.ptr<float>(y);
		float *pOrnt = m_pOrnt1f.ptr<float>(y);
		float *pDer = m_pDer1f.ptr<float>(y);
		for (int x = 0; x < m_w; x++)
		{
			compute_eigenvals(yy[x], xy[x], xx[x], eigval, eigvec);
			pOrnt[x] = (float)atan2(-eigvec[0][1], eigvec[0][0]); //计算法线方向
			if (pOrnt[x] < 0.0f)
				pOrnt[x] += PI2;
			pDer[x] = float(eigval[0] > 0.0f ? eigval[0] : 0.0f);//计算二阶导数
		}
	}

	GaussianBlur(m_pDer1f, m_pDer1f, Size(3, 3), 0);
	normalize(m_pDer1f, m_pDer1f, 0, 1, NORM_MINMAX);
	NoneMaximalSuppress(linkEndBound, linkStartBound);
	return m_pDer1f;
}
Пример #9
0
//normalized (0,1) and inverted Sobel edge detector
std::shared_ptr<Picture> SobelEdgeFilter::ApplyImpl(const std::shared_ptr<Picture>& pic)
{
	const auto& src = pic->Mat();
    double minVal, maxVal;
    cv::Mat res;
    cv::Mat XGrad, YGrad;
    cv::Mat XGradAbs, YGradAbs;
    
    Sobel( src, XGrad, Depth, 1, 0, 3, Scale, Delta, cv::BORDER_DEFAULT );
    convertScaleAbs( XGrad, XGradAbs );
    
    Sobel( src, YGrad, Depth, 0, 1, 3, Scale, Delta, cv::BORDER_DEFAULT );
    convertScaleAbs( YGrad, YGradAbs );
    
    addWeighted( XGradAbs, 0.5, YGradAbs, 0.5, 0, res );
    
    cvtColor(res, res, CV_BGR2GRAY);
    
    minMaxLoc(res, &minVal, &maxVal);
    res.convertTo(res, CV_64F,1/(maxVal - minVal));
    minMaxLoc(res, &minVal, &maxVal);
    res =  1 - res;

    return std::make_shared<Picture>(res);
}
void RidgeDetectionFilterImpl::getRidgeFilteredImage(InputArray _img, OutputArray out)
{
    Mat img = _img.getMat();
    CV_Assert(img.channels() == 1 || img.channels() == 3);

    if(img.channels() == 3)
        cvtColor(img, img, COLOR_BGR2GRAY);

    Mat sbx, sby;
    Sobel(img, sbx, _ddepth, _dx, 0, _ksize, _scale, _delta, _borderType);
    Sobel(img, sby, _ddepth, 0, _dy, _ksize, _scale, _delta, _borderType);

    Mat sbxx, sbyy, sbxy;
    Sobel(sbx, sbxx, _ddepth, _dx, 0, _ksize, _scale, _delta, _borderType);
    Sobel(sby, sbyy, _ddepth, 0, _dy, _ksize, _scale, _delta, _borderType);
    Sobel(sbx, sbxy, _ddepth, 0, _dy, _ksize, _scale, _delta, _borderType);

    Mat sb2xx, sb2yy, sb2xy;
    multiply(sbxx, sbxx, sb2xx);
    multiply(sbyy, sbyy, sb2yy);
    multiply(sbxy, sbxy, sb2xy);

    Mat sbxxyy;
    multiply(sbxx, sbyy, sbxxyy);

    Mat rootex;
    rootex = (sb2xx +  (sb2xy + sb2xy + sb2xy + sb2xy)  - (sbxxyy + sbxxyy) + sb2xy );
    Mat root;
    sqrt(rootex, root);
    Mat ridgexp;
    ridgexp = ( (sbxx + sbyy) + root );
    ridgexp.convertTo(out, _out_dtype, 0.5);
}
Пример #11
0
bool ObjPatchMatcher::PrepareViewPatchDB() {

	// get files
	patch_data.release();
	patch_meta.objects.clear();
	DirInfos cate_dirs;
	// top categories
	ToolFactory::GetDirsFromDir(uw_view_root, cate_dirs);	
	cate_dirs.erase(cate_dirs.begin(), cate_dirs.begin()+5);
	cate_dirs.erase(cate_dirs.begin()+1, cate_dirs.end());
	for(size_t i=0; i<cate_dirs.size(); i++) {
		DirInfos sub_cate_dirs;
		ToolFactory::GetDirsFromDir(cate_dirs[i].dirpath, sub_cate_dirs);
		for(auto cur_sub_dir : sub_cate_dirs) {
			FileInfos cate_fns;
			ToolFactory::GetFilesFromDir(cur_sub_dir.dirpath, "*_crop.png", cate_fns);
			for (auto cur_fn : cate_fns) {
				VisualObject cur_obj_view;
				cur_obj_view.meta_data.category_id = i;
				cur_obj_view.meta_data.img_path = cur_fn.filepath;
				cur_obj_view.meta_data.img_file = cur_fn.filename;
				cur_obj_view.meta_data.dmap_path = cur_fn.filepath.substr(0, cur_fn.filepath.length()-7) + "depthcrop.png";
				patch_meta.objects.push_back(cur_obj_view);
			}
		}

		cout<<"Loaded category: "<<cate_dirs[i].dirpath<<endl;
	}

	// get features
	cout<<"Extracting view features..."<<endl;
	for(size_t i=0; i<patch_meta.objects.size(); i++) {
		VisualObject& cur_obj = patch_meta.objects[i];

		Mat vimg = imread(cur_obj.meta_data.img_path);
		//Mat dmap = imread(cur_obj_view.dmap_path, CV_LOAD_IMAGE_UNCHANGED);
		resize(vimg, vimg, patch_size);
		Mat gray_img_float, grad_x, grad_y, grad_mag;
		cvtColor(vimg, gray_img_float, CV_BGR2GRAY);
		gray_img_float.convertTo(gray_img_float, CV_32F);
		Sobel(gray_img_float, grad_x, CV_32F, 1, 0);
		Sobel(gray_img_float, grad_y, CV_32F, 0, 1);
		magnitude(grad_x, grad_y, grad_mag);
		grad_mag.copyTo(cur_obj.visual_data.custom_feats["gradient"]);

		Mat cur_feat;
		ComputePatchFeat(cur_obj.visual_data.custom_feats, cur_feat);
		patch_data.push_back(cur_feat);

		cout<<i<<"/"<<patch_meta.objects.size()<<endl;
	}

	cout<<"Feature extraction done."<<endl;

	cout<<"Database ready."<<endl;

	return true;

}
    /**
     * The final selected edges for kernel estimation are determined as:
     * ∇I^s = ∇I · H (M * ||∇I||_2 − τ_s )
     * where H is the Heaviside step function and M = = H(r - τ_r)
     * 
     * @param image      input image which will be shockfiltered (I)
     * @param confidence mask for ruling out some pixel (r)
     * @param r          threshold for edge mask (value should be in range [0,1]) (τ_r)
     * @param s          threshold for edge selection (value should be in range [0, 200]) (τ_s)
     * @param selection  result (∇I^s)
     */
    void selectEdges(const Mat& image, const Mat& confidence, const float r, const float s, array<Mat,2>& selection) {
        assert(image.type() == CV_8U && "gray value image needed");

        // create mask for ruling out pixel belonging to small confidence-values
        // M = H(r - τ_r) where H is Heaviside step function
        Mat mask;
        inRange(confidence, r + 0.00001, 1, mask);  // add a very small value to r to exclude zero values

        // shock filter the input image
        Mat shockImage;
        coherenceFilter(image, shockImage);

        // #ifndef NDEBUG
        //      imshow("shock filter", shockImage);
        // #endif

        // gradients of shock filtered image
        const int delta = 0;
        const int ddepth = CV_32F;
        const int ksize = 3;
        const int scale = 1;

        // compute gradients x/y
        array<Mat,2> gradients;
        Sobel(shockImage, gradients[0], ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT);
        Sobel(shockImage, gradients[1], ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);

        // normalize gradients
        normalizeOne(gradients);

        // #ifndef NDEBUG
        //     showGradients("x gradient shock", gradients[0]);
        //     showGradients("y gradient shock", gradients[1]);
        // #endif

        // save gradients of the final selected edges
        selection = { Mat::zeros(image.rows, image.cols, CV_32F),
                      Mat::zeros(image.rows, image.cols, CV_32F) };

        for (int x = 0; x < gradients[0].cols; x++) {
            for (int y = 0; y < gradients[0].rows; y++) {
                // if the mask is zero at the current coordinate the result
                // of the equation (see method description) is zero too.
                // So nothing has to be computed for this case
                if (mask.at<uchar>(y, x) != 0) {
                    Vec2f gradient = { gradients[0].at<float>(y,x),
                                       gradients[1].at<float>(y,x) };

                    // if the following equation doesn't hold the value
                    // is also zero and nothing has to be computed
                    if ((norm(gradient[0], gradient[1]) - s) > 0) {
                        selection[0].at<float>(y,x) = gradient[0];
                        selection[1].at<float>(y,x) = gradient[1];
                    }
                }
            }
        }
    }
Пример #13
0
float calcBlurriness(const Mat &frame)
{
    Mat Gx, Gy;
    Sobel(frame, Gx, CV_32F, 1, 0);
    Sobel(frame, Gy, CV_32F, 0, 1);
    double normGx = norm(Gx);
    double normGy = norm(Gy);
    double sumSq = normGx*normGx + normGy*normGy;
    return static_cast<float>(1. / (sumSq / frame.size().area() + 1e-6));
}
Пример #14
0
void getHOGFeatures1(Mat InputImage, Mat & Histogram) {
	Mat gradH, gradV, imageO, imageM;

	Sobel(InputImage, gradH, DataType<float>::type, 1, 0, 3, 1.0, 0.0, BORDER_DEFAULT);
	Sobel(InputImage, gradV, DataType<float>::type, 0, 1, 3, 1.0, 0.0, BORDER_DEFAULT);

	imageM.create(InputImage.rows, InputImage.cols, DataType<float>::type);
	imageO.create(InputImage.rows, InputImage.cols, DataType<float>::type);

	// calculate magnitude and orientation images...
	float maxM = 0;
	int r, c;
	for (r=0;r<InputImage.rows;r++) {
		for (c=0;c<InputImage.cols;c++) {
			imageO.at<float>(r,c) = (float)(atan2(gradV.at<float>(r,c),gradH.at<float>(r,c)));
			imageM.at<float>(r,c) = gradH.at<float>(r,c)*gradH.at<float>(r,c) + gradV.at<float>(r,c)*gradV.at<float>(r,c);
			if (imageM.at<float>(r,c)>maxM) maxM = imageM.at<float>(r,c);
		}
	}
	// normalize magnitude image to 1...
	for (r=0;r<InputImage.rows;r++) {
		for (c=0;c<InputImage.cols;c++) {
			imageM.at<float>(r,c) /= maxM;
		}
	}

	// form the histogram - will get rid of small magnitude orientations
	Histogram.create(1, 180, DataType<int>::type);
	for(c=0; c<Histogram.cols; c++) {
		Histogram.at<int>(0,c) = 0;
	}
	float stepSize = (float)(2.0*PI/(float)Histogram.cols);
	for (r=3;r<InputImage.rows-3;r++) {
		for (c=3;c<InputImage.cols-3;c++) {
			if (imageM.at<float>(r,c)>MINIMUM_GRAD_MAGNITUDE_FOR_ORIENTATION) {
				float theta = imageO.at<float>(r,c); // between -pi and pi...
				theta += (float)PI;
				int count = (int)(theta / stepSize);
				if (count>=0 && count<Histogram.cols) Histogram.at<int>(0,count) += 1;
			}
			else {
			}
		}
	}

	//imshow("Orient Image", imageO); imshow("Magnit Image", imageM); cv::waitKey(0);

	FILE * fileHist = fopen("hist.txt","w");
	for(c=0; c<Histogram.cols; c++) {
		fprintf(fileHist, "%d ", Histogram.at<int>(0,c));
	}
	fprintf(fileHist, "\n");
	fclose(fileHist);
} // end-getHOGFeatures1
Пример #15
0
std::vector<PBASFeature> PBASFeature::calcFeatureMap(const cv::Mat& inputFrame)
{
	std::vector<PBASFeature> result;
	// get gradient magnitude map
	cv::Mat sobelX, sobelY, inputGray, gradMagnMap;

	cv::GaussianBlur(inputFrame, inputGray, cv::Size(5, 5), 2, 2, cv::BORDER_DEFAULT);
	cv::cvtColor(inputGray, inputGray, CV_BGR2GRAY);
	
	// Gradient Y
	Sobel(inputGray, sobelY, CV_64F, 1, 0, 5, 1, 0, cv::BORDER_DEFAULT);
	convertScaleAbs(sobelY, sobelY);

	// Gradient X
	Sobel(inputGray, sobelX, CV_64F, 0, 1, 5, 1, 0, cv::BORDER_DEFAULT);
	convertScaleAbs(sobelX, sobelX);

	sobelY.convertTo(sobelY, CV_8U);
	sobelX.convertTo(sobelX, CV_8U);

	int kernelSize = 7;
	cv::Mat avgKernel = cv::Mat::ones(kernelSize, kernelSize, CV_32F) / (kernelSize * kernelSize);
	cv::Mat avgMatB, avgMatG, avgMatR;
	cv::Mat channel[3];

	cv::split(inputFrame, channel);

	cv::filter2D(channel[0], avgMatB, CV_8U, avgKernel, cv::Point(-1, -1), 0, cv::BORDER_DEFAULT);
	cv::filter2D(channel[1], avgMatG, CV_8U, avgKernel, cv::Point(-1, -1), 0, cv::BORDER_DEFAULT);
	cv::filter2D(channel[2], avgMatR, CV_8U, avgKernel, cv::Point(-1, -1), 0, cv::BORDER_DEFAULT);

	cv::Mat tmp(1, 1, CV_8UC3);

	for (int y = 0; y < inputFrame.rows; ++y)
	{
		for (int x = 0; x < inputFrame.cols; ++x)
		{

			tmp.ptr<uchar>(0)[0] = inputFrame.at<cv::Vec3b>(y, x)[0];
			tmp.ptr<uchar>(0)[1] = inputFrame.at<cv::Vec3b>(y, x)[1];
			tmp.ptr<uchar>(0)[2] = inputFrame.at<cv::Vec3b>(y, x)[2];



			result.push_back(PBASFeature(tmp, avgMatB.at<uchar>(y, x), avgMatG.at<uchar>(y, x), avgMatR.at<uchar>(y, x), sobelX.at<uchar>(y,x), sobelY.at<uchar>(y, x)));
		}


	}
	return result;

}
Пример #16
0
void Canny(Mat src, Mat& dst, int thres)
{
    Mat mdx, mdy;

    printf("Canny thresheold %d\n", thres);

    Sobel(src, mdx, CV_64F, 1, 0);
    Sobel(src, mdy, CV_64F, 0, 1);

    dst.create(src.rows, src.cols, CV_64F);

    int x, y;
    double dx, dy;
    double mag, orien;

    //
    // Compute edge response
    //
    for(x = 0; x < src.cols; x++)
        for(y = 0; y < src.rows; y++) {
            dx = mdx.at<double>(y, x);
            dy = mdy.at<double>(y, x);
            dst.at<double>(y, x) = hypot(dx, dy);                    // Gradient magnitude
        }

    //
    // Max supression
    //
    for(x = 0; x < src.cols; x++)
        for(y = 0; y < src.rows; y++) {
            dx = mdx.at<double>(y, x);
            dy = mdy.at<double>(y, x);
            mag = dst.at<double>(y,x);
            orien = atan2(dx, dy);                                  // Gradient direction in radians

            if(mag > thres) {
                double delta_x = cos(orien);
                double delta_y = sin(orien);
                double pixel;

                pixel = BilinearInterpolation(dst, x + delta_x, y + delta_y);           // e0 is one pixel away in the gradient direction
                if(mag > pixel) {
                   pixel = BilinearInterpolation(dst, x - delta_x, y - delta_y);        // e1 is one pixel away in the opposite direction to the gradient
                   if(mag > pixel)
                        continue;
                }
            }

            dst.at<double>(y, x) = 0;                               // Supress
        }
}
Пример #17
0
void GrdCC::buildCV( const Mat& lImg, const Mat& rImg, const int maxDis, Mat* costVol )
{
	// for TAD + Grd input image must be CV_64FC3
	CV_Assert( lImg.type() == CV_64FC3 && rImg.type() == CV_64FC3 );

	int hei = lImg.rows;
	int wid = lImg.cols;
	Mat lGray, rGray;
	Mat lGrdX, rGrdX;
	Mat tmp;
	lImg.convertTo( tmp, CV_32F );
	cvtColor( tmp, lGray, CV_RGB2GRAY );
	rImg.convertTo( tmp, CV_32F );
	cvtColor( tmp, rGray, CV_RGB2GRAY );
	// X Gradient
	// sobel size must be 1
	Sobel( lGray, lGrdX, CV_64F, 1, 0, 1 );
	Sobel( rGray, rGrdX, CV_64F, 1, 0, 1 );

	// try your own gradient
	//myComputeGradient( lGray, lGrdX );
	//myComputeGradient( rGray, rGrdX );

	// build cost volume! start from 1
	// try 0
	for( int d = 0; d < maxDis; d ++ ) {
		printf( "-c-c-" );
		for( int y = 0; y < hei; y ++ ) {
			double* lData = ( double* ) lImg.ptr<double>( y );
			double* rData = ( double* ) rImg.ptr<double>( y );
			double* lGData = ( double* ) lGrdX.ptr<double>( y );
			double* rGData = ( double* ) rGrdX.ptr<double>( y );
			double* cost   = ( double* ) costVol[ d  ].ptr<double>( y );
			for( int x = 0; x < wid; x ++ ) {
				if( x - d >= 0 ) {
					double* lC = lData + 3 * x;
					double* rC = rData + 3 * ( x - d );
					double* lG = lGData + x;
					double* rG = rGData + x - d;
					cost[ x ] = myCostGrd( lC, rC, lG, rG );
				} else {
					double* lC = lData + 3 * x;
					double* lG = lGData + x;
					cost[ x ] = myCostGrd( lC, lG );
				}

			}
		}
	}
}
Пример #18
0
int EdgeHandle::singleEdgeHandle( string InImgPosition , string InImageName, string OutImgPosition, string OutImgName ){

	Mat src, src_gray;
	Mat grad;
	int scale = 1;
	int delta = 0;
	int ddepth = CV_16S;

	int c;

	/// Load an image
	src = imread(InImgPosition+InImageName);
	if( !src.data )
	{ return -1; }

	GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );

	/// Convert it to gray
	cvtColor( src, src_gray, CV_RGB2GRAY );

	/// Generate grad_x and grad_y
	Mat grad_x, grad_y;
	Mat abs_grad_x, abs_grad_y;

	//sobel
	Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
	convertScaleAbs( grad_x, abs_grad_x );
	Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
	convertScaleAbs( grad_y, abs_grad_y );
	addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

	// binary image.
	//threshold(grad,grad,0,255,THRESH_BINARY);

	// Apply the specified morphology operation
	int morph_size = 1;
	Mat element = getStructuringElement( 1, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) );
	morphologyEx( grad, grad, 5, element );
	morphologyEx( grad, grad, 5, element );
	morphologyEx( grad, grad, 5, element );

	// Create window
	string outs = OutImgPosition + OutImgName;
	imwrite(outs,grad);

	return 0;

}
Пример #19
0
void my_Sobel(cv::Mat& srcImg, cv::Mat& dstImg)
{
    Mat dst_x, dst_y;
    Sobel(srcImg, dst_x, srcImg.depth(), 1, 0, 3);
    Sobel(srcImg, dst_y, srcImg.depth(), 0, 1, 3);
    convertScaleAbs(dst_x, dst_x);
    convertScaleAbs(dst_y, dst_y);
    addWeighted( dst_x, 0.5, dst_y, 0.5, 0, dstImg);

    if(dstImg.channels() == 3)
    {
        cvtColor(dstImg, dstImg, CV_BGR2GRAY);
        cvtColor(dstImg, dstImg, CV_GRAY2BGR);
    }

}
void	DetectRegions::UseSobel(const cv::Mat& input, cv::Mat& img_threshold)
{
  D_IMG_SAVE( input, "00_img_input.png" );


  //convert image to gray
  cv::Mat img_gray;
  cvtColor(input, img_gray, CV_BGR2GRAY);
  cv::blur( img_gray, img_gray, cv::Size(5,5) );

  D_IMG_SAVE( img_gray, "01_img_Gray.png" );



  //Find vertical lines. Car plates have high density of vertical lines
  cv::Mat img_sobel;
  Sobel(img_gray, img_sobel, CV_8U, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);

  D_IMG_SAVE( img_sobel, "02_img_Sobel.png" );


  //threshold image
  threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);

  D_IMG_SAVE( img_threshold, "03_img_Threshold.png" );


  //Morphplogic operation close
  cv::Mat element = getStructuringElement(cv::MORPH_RECT, cv::Size(17, 3) );
  morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element);

  D_IMG_SAVE( img_threshold, "04_img_Close.png" );

}
Пример #21
0
void clsStencilArt::StencilArtMain(IplImage **pImgSrc, int bPreviewMode, int*iErrCode,GlobalHelper &g_GH)
{
	pImgSource = *pImgSrc;
	if(!pImgSource)
	{
		//g_GH.DebugOut("StencilArtMain: Source Image NULL",FALSE);
		*iErrCode = 1;
		return;
	}

	iNC = pImgSource->width;
	iNR = pImgSource->height;
	
	ppfConv = g_GH.FloatHeap2D(iNR,iNC);
	if(!ppfConv)
	{
		//g_GH.DebugOut("clsSobel::Sobel - FloatHelp2D failed",FALSE);
		return;
	}

	IplImage * pImgGrey = g_GH.cvCreateImageProxy(cvSize(iNC,iNR),pImgSource->depth,1);
	g_GH.cvCvtColorProxy(pImgSource,pImgGrey,CV_BGR2GRAY);
	g_GH.cvReleaseImageProxy(&pImgSource);
	pImgSource = pImgGrey;

	ApplyCanvas(g_GH);
	Sobel(g_GH);

	*pImgSrc = pImgDest;
	g_GH.cvReleaseImageProxy(&pImgSource);
	free(ppfConv[0]);free(ppfConv);


}
Пример #22
0
void harrisCornerDetection() {
	cv::Mat grad_x, grad_y;
	cv::Mat abs_grad_x, abs_grad_y;
	cv::Mat grad;
	cv::Mat R(inputImage.rows,inputImage.cols,CV_64F);
	double maxR = 0;
	Sobel(inputImage,grad_x,CV_16S,1,0,3,1,0,cv::BORDER_DEFAULT);
	Sobel(inputImage,grad_y,CV_16S,0,1,3,1,0,cv::BORDER_DEFAULT);
	convertScaleAbs(grad_x,abs_grad_x);
	convertScaleAbs(grad_y,abs_grad_y);
	addWeighted(abs_grad_x,0.5,abs_grad_y,0.5,0,grad);
	for (int x=0;x<inputImage.cols;x++) {
		for (int y=0;y<inputImage.rows;y++) {
			double A = 0;
			double B = 0;
			double C = 0;
			for (int u=-templateSize/2;u<=templateSize/2;u++) {
				for (int v=-templateSize/2;v<=templateSize/2;v++) {
					int columnIndex = x+u;
					int rowIndex = y+v;
					if (columnIndex<0 || columnIndex>=inputImage.cols) {
						continue;
					}
					if (rowIndex<0 || rowIndex>=inputImage.rows) {
						continue;
					}
					A += pow(abs_grad_x.at<uchar>(rowIndex,columnIndex),2);
					B += abs_grad_x.at<uchar>(rowIndex,columnIndex)*abs_grad_y.at<uchar>(rowIndex,columnIndex);
					C += pow(abs_grad_y.at<uchar>(rowIndex,columnIndex),2);
				}
			}
			double cornerness = A*C-B*B-0.04*(A+C);
			R.at<double>(y,x) = cornerness;
			maxR = std::max(cornerness,maxR);
		}
	}
	cv::Mat annotatedImage;
	cv::cvtColor(inputImage,annotatedImage,CV_GRAY2RGB);
	for (int x=0;x<inputImage.cols;x++) {
		for (int y=0;y<inputImage.rows;y++) {
			if (R.at<double>(y,x) >= maxR*threshold) {
				circle(annotatedImage,cvPoint(x,y),2,CV_RGB(0,0,255));
			}
		}
	}
	imshow("Harris Corner Detection",annotatedImage);
}
CMat& CMat::soble(){
	Mat src_gray;
	pic.copyTo(src_gray);
	pic.release();
	int scale = 1,delta = 0,ddepth = CV_16S;
	Mat grad_x, grad_y;
	Mat abs_grad_x, abs_grad_y;
	Sobel(src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
	convertScaleAbs(grad_x, abs_grad_x );
	Sobel(src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
	convertScaleAbs(grad_y, abs_grad_y);
	addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, pic);
	src_gray.release();
	grad_x.release(), grad_y.release();
	abs_grad_x.release(), abs_grad_y.release();
	return (*this);
}
Пример #24
0
	void coherenceEnhancingShockFilter(cv::InputArray src, cv::OutputArray dest_, const int sigma, const int str_sigma_, const double blend, const int iter)
	{
		Mat dest = src.getMat();
		const int str_sigma = min(31, str_sigma_);

		for (int i = 0; i < iter; i++)
		{
			Mat gray;
			if (src.channels() == 3)cvtColor(dest, gray, CV_BGR2GRAY);
			else gray = dest;

			Mat eigen;
			if (gray.type() == CV_8U || gray.type() == CV_32F || gray.type() == CV_64F)
				cornerEigenValsAndVecs(gray, eigen, str_sigma, 3);
			else
			{
				Mat grayf; gray.convertTo(grayf, CV_32F);
				cornerEigenValsAndVecs(grayf, eigen, str_sigma, 3);
			}

			vector<Mat> esplit(6);
			split(eigen, esplit);
			Mat x = esplit[2];
			Mat y = esplit[3];
			Mat gxx;
			Mat gyy;
			Mat gxy;
			Sobel(gray, gxx, CV_32F, 2, 0, sigma);
			Sobel(gray, gyy, CV_32F, 0, 2, sigma);
			Sobel(gray, gxy, CV_32F, 1, 1, sigma);

			Mat gvv = x.mul(x).mul(gxx) + 2 * x.mul(y).mul(gxy) + y.mul(y).mul(gyy);

			Mat mask;
			compare(gvv, 0, mask, cv::CMP_LT);

			Mat di, ero;
			erode(dest, ero, Mat());
			dilate(dest, di, Mat());
			di.copyTo(ero, mask);
			addWeighted(dest, blend, ero, 1.0 - blend, 0.0, dest);
		}
		dest.copyTo(dest_);
	}
bool IPLGradientOperator::sobel(IPLImage* image)
{
   int width = image->width();
   int height = image->height();

   const int kSize = 3;

   // fast gradient
   int progress = 0;
   int maxProgress = height*width;

   notifyProgressEventHandler(-1);

   cv::Mat input;
   cv::Mat gX;
   cv::Mat gY;
   cvtColor(image->toCvMat(),input,CV_BGR2GRAY);
 
   Sobel(input,gX,CV_32F,1,0,kSize);
   Sobel(input,gY,CV_32F,0,1,kSize);
 
   for(int x=1; x<width; x++)
   {
       for(int y=1; y<height; y++)
       {
       // progress
       notifyProgressEventHandler(100*progress++/maxProgress);
         ipl_basetype gx = gX.at<cv::Vec<float,1>>(y,x).val[0] * FACTOR_TO_FLOAT ;
         ipl_basetype gy = gY.at<cv::Vec<float,1>>(y,x).val[0] * FACTOR_TO_FLOAT ;
         
         double phase = (gx!=0.0 || gy!=0.0 )? atan2( -gy, gx ) : 0.0;
 
         while( phase > 2.0 * PI ) phase -= 2.0 * PI;
         while( phase < 0.0 ) phase += 2.0 * PI;
 
         phase /= 2.0 * PI;
 
         _result->phase(x,y) = phase;
         _result->magnitude(x,y) = sqrt(gx*gx + gy*gy);
       }
    }
    return true;
}
Пример #26
0
        void apply(const cv::Mat& A, cv::Mat &B)
        {
            Sobel(A, grad_x, ddepth, 1, 0, kernel, scale, delta, cv::BORDER_DEFAULT);
            Sobel(A, grad_y, ddepth, 0, 1, kernel, scale, delta, cv::BORDER_DEFAULT);

            Sobel(grad_x, d_xx, ddepth, 1, 0, kernel, scale, delta, cv::BORDER_DEFAULT);
            Sobel(grad_x, d_xy, ddepth, 0, 1, kernel, scale, delta, cv::BORDER_DEFAULT);
            Sobel(grad_y, d_yy, ddepth, 0, 1, kernel, scale, delta, cv::BORDER_DEFAULT);

            diskr = (((d_xx - d_yy) / 2.0).mul(((d_xx - d_yy) / 2.0)) + d_xy.mul(d_xy));
            sqrt(diskr, root);
            largeC = (d_xx + d_yy) / 2.0 + root;
            smallC = (d_xx + d_yy) / 2.0 - root;

            switch (output)
            {
                case picDx:
                    normalize(grad_x, B, 0, 1, CV_MINMAX);
                    break;
                case picDy:
                    normalize(grad_y, B, 0, 1, CV_MINMAX);
                    break;
                case picDxx:
                    normalize(d_xx, B, 0, 1, CV_MINMAX);
                    break;
                case picDxy:
                    normalize(d_xy, B, 0, 1, CV_MINMAX);
                    break;
                case picDyy:
                    normalize(d_yy, B, 0, 1, CV_MINMAX);
                    break;
                case picEVSmall:
                    normalize(smallC, B, 0, 1, CV_MINMAX);
                    break;
                case picEVLarge:
                    normalize(largeC, B, 0, 1, CV_MINMAX);
                    break;
                case picDerivInput:
                default:
                    B = A;
                    break;
            }
        }
Пример #27
0
Mat EdgeHandle::MatIllumination(Mat img){

	Mat src, src_gray;
	Mat grad;
	int scale = 1;
	int delta = 0;
	int ddepth = CV_16S;

	int c;

	/// Load an image
	src = img;

	GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );

	/// Convert it to gray
	cvtColor( src, src_gray, CV_RGB2GRAY );

	/// Generate grad_x and grad_y
	Mat grad_x, grad_y;
	Mat abs_grad_x, abs_grad_y;

	//sobel
	Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
	convertScaleAbs( grad_x, abs_grad_x );
	Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
	convertScaleAbs( grad_y, abs_grad_y );
	addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

	// binary image.
	//threshold(grad,grad,0,255,THRESH_BINARY);

	// Apply the specified morphology operation
	int morph_size = 1;
	Mat element = getStructuringElement( 1, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) );
	morphologyEx( grad, grad, 5, element );
	morphologyEx( grad, grad, 5, element );
	morphologyEx( grad, grad, 5, element );

	return grad;

}
Пример #28
0
void testApp::updateTrianglesRandom() {
	Mat mat = Mat(kinect.getHeight(), kinect.getWidth(), CV_32FC1, kinect.getDistancePixels());
	
	Sobel(mat, sobelxy, CV_32F, 1, 1);
	
	sobelxy = abs(sobelxy);
	int randomBlur = panel.getValueI("randomBlur") * 2 + 1;
	boxFilter(sobelxy, sobelbox, 0, cv::Size(randomBlur, randomBlur), Point2d(-1, -1), false);
	
	triangulator.reset();
	points.clear();
	int i = 0;
	attempts = 0;
	int randomCount = panel.getValueI("randomCount");
	float randomWeight = panel.getValueF("randomWeight");
	while(i < randomCount) {
		Point2d curPosition(1 + (int) ofRandom(sobelbox.cols - 3), 
												1 + (int) ofRandom(sobelbox.rows - 3));
		float curSample = sobelbox.at<unsigned char>(curPosition) / 255.f;
		float curGauntlet = powf(ofRandom(0, 1), 2 * randomWeight);
		if(curSample > curGauntlet) {
			points.push_back(toOf(curPosition));
			triangulator.addPoint(curPosition.x, curPosition.y, 0);
			sobelbox.at<unsigned char>(curPosition) = 0; // don't do the same point twice
			i++;
		}
		attempts++;
		if(i > attempts * 100) {
			break;
		}
	}
	
	// add the edges
	int w = mat.cols;
	int h = mat.rows;
	for(int x = 0; x < w; x++) {
		triangulator.addPoint(x, 0, 0);
		triangulator.addPoint(x, h - 1, 0);
	}
	for(int y = 0; y < h; y++) {
		triangulator.addPoint(0, y, 0);
		triangulator.addPoint(w - 1, y, 0);
	}
	
	triangulator.triangulate();
	
	int n = triangulator.triangles.size();
	triangles.resize(n);
	for(int i = 0; i < n; i++) {
		triangles[i].vert3 = triangulator.triangles[i].points[0];
		triangles[i].vert2 = triangulator.triangles[i].points[1];
		triangles[i].vert1 = triangulator.triangles[i].points[2];
	}
}
Пример #29
0
void FWImage::createGrad(int scale,int delta)
{
    /// Generate grad_x and grad_y
    Mat grad_x, grad_y;
    Mat abs_grad_x, abs_grad_y;
    int ddepth = CV_16S;
    
    /// Gradient X
    //Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
    Sobel( this->iGray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
    convertScaleAbs( grad_x, abs_grad_x );
    
    /// Gradient Y
    //Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
    Sobel( this->iGray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
    convertScaleAbs( grad_y, abs_grad_y );
    
    /// Total Gradient (approximate)
    addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, this->iGrad );
    
}
Пример #30
0
/**
 * Efeito de pintura.
 */
void edgeDetectSobel(Mat& img) {
	Mat gray, temp;
	GaussianBlur(img, img, Size(3, 3), 0, 0);
	cvtColor(img, gray, CV_RGB2GRAY);
	//x
	Sobel(gray, img, CV_16S, 1, 0, 3, 1.5, 0.3, BORDER_DEFAULT);

	convertScaleAbs(img, img);
	//y
	Sobel(gray, temp, CV_16S, 0, 1, 3, 1.5, 0.4, BORDER_DEFAULT);

	convertScaleAbs(temp, temp);
	addWeighted(img, .3, temp, .3, .3, img);

	erode(img, img,
			getStructuringElement(MORPH_ELLIPSE,
					Size(dilation_size + 1, dilation_size + 1),
					Point(dilation_size, dilation_size)));
	bitwise_not(img, img);
	cvtColor(img, img, CV_GRAY2BGR);

}