Exemplo n.º 1
0
void performHighPass(const cv::Mat& image, cv::Mat& res, int rad) {
    cv::Mat grey, tmp;
    cv::cvtColor(image, grey, CV_BGR2GRAY);


    grey.convertTo(grey, CV_32F);
    grey.copyTo(res);
    res.convertTo(res, CV_8U);
    std::vector<cv::Mat> planes(2, cv::Mat());
    std::vector<cv::Mat> polar(2, cv::Mat());

    cv::dft(grey, tmp, cv::DFT_COMPLEX_OUTPUT);
    cv::split(tmp, planes);
    cv::cartToPolar(planes[0], planes[1], polar[0], polar[1]);
    visualization(polar[0], tmp);
    concatImages(res, tmp, res);

    rearrangeQuadrants(polar[0]);
    highPassFilter(polar[0], rad);
    rearrangeQuadrants(polar[0]);

    visualization(polar[0], tmp);
    tmp.convertTo(tmp, res.type());
    concatImages(res, tmp, res);

    cv::polarToCart(polar[0], polar[1], planes[0], planes[1]);
    cv::merge(planes, tmp);
    cv::dft(tmp, tmp, cv::DFT_SCALE | cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT);
    tmp.convertTo(tmp, CV_8U);
    concatImages(res, tmp, res);
}
Exemplo n.º 2
0
DisplayWindow *LowPassFilter::apply(QString windowBaseName)
{
	Q_ASSERT(mCA->dimensionality == 3);
	QVector2D center(mCA->shape()[1] / 2, mCA->shape()[2] / 2);
	rearrangeQuadrants();
	for (unsigned int i = 0; i < mCA->shape()[0]; i++) {
		for (unsigned int j = 0; j < mCA->shape()[1]; j++) {
			for (unsigned int k = 0; k < mCA->shape()[2]; k++) {
				QVector2D point(j, k);
				if ((point - center).lengthSquared() >= mRadius) {
					(*mCA)[i][j][k] = Complex();
				}
			}
		}
	}
	rearrangeQuadrants();
	int layers = mCA->shape()[0];
	int w = mCA->shape()[1];
	int h = mCA->shape()[2];
	ComplexArray *ca = new ComplexArray(boost::extents[layers][w][h]);
	*ca = *mCA;
	return new TransformWindow(ca, mFormat, windowBaseName + ", " + name(), q_check_ptr(qobject_cast<QWidget *>(parent()->parent())));
}
Exemplo n.º 3
0
int main(int argc, char* argv[]) {
	//if OpenGL is enabled open an OpenGL window, otherwise just a regular window
	cv::namedWindow("magnitude",cv::WINDOW_AUTOSIZE);
	if(argc != 2) {
		std::cout<<"Usage: ./part1.cpp image"<<std::endl;
		return 0;
	}

	cv::Mat input_image = cv::imread(argv[1],CV_LOAD_IMAGE_GRAYSCALE);

	if(input_image.empty()) {
		char appended[100];
		appended[0] = '\0';
		strcat(appended,"../");
		strcat(appended,argv[1]);
		cv::Mat input_image = cv::imread(appended,CV_LOAD_IMAGE_GRAYSCALE);
		if(input_image.empty()) {
			std::cout<<argv[1]<<" was invalid"<<std::endl;
			std::cout<<" also tried: "<<appended<<std::endl;
			return 0;
		}
	}

	//the image dimensions must be a power of two
	cv::Mat padded_image;
	cv::Size padded_size(
			cv::getOptimalDFTSize(input_image.cols),
			cv::getOptimalDFTSize(input_image.rows));

	//pad the input image
	cv::copyMakeBorder(input_image, //input image
			padded_image, //output image
			0, //pad the top with..
			padded_size.height-input_image.rows, //pad the bottom with...
			0, //pad the left with...
			padded_size.width-input_image.cols, //pad the right with...
			cv::BORDER_CONSTANT, //make the border constant (as opposed to a copy of the data
			cv::Scalar::all(0)); //make the border black

	/*
	 * The DFT function expects a two-channel image, so let's make two planes
	 * and then merge them
	 */

	cv::Mat planes[] = {cv::Mat_<float>(padded_image),cv::Mat::zeros(padded_size,CV_32F)};

	//now make a single complex (two-channel) image
	cv::Mat complex_image;
	cv::merge(planes,2,complex_image);

	//get the dft of the image
	cv::dft(complex_image,complex_image);

	//generate the first mask
	cv::Mat first_mask(padded_size,complex_image.type(),cv::Scalar::all(0));

	//get roi in the center of the mask
	cv::Rect first_roi(first_mask.cols/2-1,first_mask.rows/2-1,3,3);
	std::cout<<"roi: "<<first_roi<<std::endl;
	cv::Mat first_mask_center(first_mask,first_roi);
	first_mask_center.at<cv::Vec2f>(0,0)[0] = -1;
	first_mask_center.at<cv::Vec2f>(1,0)[0] = -2;
	first_mask_center.at<cv::Vec2f>(2,0)[0] = -1;
	first_mask_center.at<cv::Vec2f>(0,2)[0] = 1;
	first_mask_center.at<cv::Vec2f>(1,2)[0] = 2;
	first_mask_center.at<cv::Vec2f>(2,2)[0] = 1;

	//transform the mask to the fourier domain
	cv::dft(first_mask,first_mask);

	//filter the image
	cv::Mat first_filtered_image = multiplyInTimeDomain(complex_image,first_mask);

	//generate the second mask
	cv::Mat second_mask(padded_size,complex_image.type(),cv::Scalar::all(0));

	//get roi in the center of the mask
	cv::Rect second_roi(second_mask.cols/2-1,second_mask.rows/2-1,3,3);
	std::cout<<"roi: "<<second_roi<<std::endl;
	cv::Mat second_mask_center(second_mask,second_roi);
	second_mask_center.at<cv::Vec2f>(0,0)[0] = -1;
	second_mask_center.at<cv::Vec2f>(0,1)[0] = -2;
	second_mask_center.at<cv::Vec2f>(0,2)[0] = -1;
	second_mask_center.at<cv::Vec2f>(2,0)[0] = 1;
	second_mask_center.at<cv::Vec2f>(2,1)[0] = 2;
	second_mask_center.at<cv::Vec2f>(2,2)[0] = 1;
	std::cout<<second_mask_center<<std::endl;

	//transform the mask to the fourier domain
	cv::dft(second_mask,second_mask);

	//filter the image
	cv::Mat second_filtered_image = multiplyInTimeDomain(complex_image,second_mask);

	//convert filtered image back to spatial domain
    cv::dft(second_filtered_image,second_filtered_image,cv::DFT_INVERSE|cv::DFT_REAL_OUTPUT);
    cv::dft(first_filtered_image,first_filtered_image,cv::DFT_INVERSE|cv::DFT_REAL_OUTPUT);

	rearrangeQuadrants(&second_filtered_image);
	rearrangeQuadrants(&first_filtered_image);

    //normalize(first_filtered_image, first_filtered_image, 0, 1, CV_MINMAX);
    //normalize(second_filtered_image, second_filtered_image, 0, 1, CV_MINMAX);

    //compute magnitude

    cv::Mat result(first_filtered_image.rows,first_filtered_image.cols,CV_32FC1,cv::Scalar::all(0));
	for(int y=0;y<result.rows;y++) {
		for(int x=0;x<result.cols;x++) {
			float first = first_filtered_image.at<float>(y,x);
			float second = second_filtered_image.at<float>(y,x);
			result.at<float>(y,x) = sqrt(first*first+second*second);
		}
	}

    normalize(result, result, 0, 1, CV_MINMAX);
    cv::imshow("first filtered image",first_filtered_image);
    cv::imshow("second filtered image",second_filtered_image);
    cv::imshow("result",result);


    //show opencv sobel
    cv::Sobel(input_image,input_image,-1,1,0);
    imshow("opencv sobel",input_image);
   cv::waitKey(0);
    return 0;
}