Beispiel #1
0
void Window::SetTestImage()
{
  QImage input_image(QString(":/my-test-image.jpg"));
  if (input_image.isNull())
     {
      qWarning() << "Картинка не загрузилась, это фатально.";
      QApplication::closeAllWindows();
      QApplication::exit(1);
  }
  GLWidget *glw = qobject_cast<GLWidget *>(myGLWidget);
  glw->SetImage(input_image);
}
Beispiel #2
0
void ImageViewer::open()
{
    if (!fileName.isEmpty()) {
      QImage input_image(fileName);
      QImage image = input_image.scaledToWidth(input_image.width() * scaleFactor);
      if (image.isNull()) {
	QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName));
	exit(0);
      }
      imageLabel->setPixmap(QPixmap::fromImage(image));
      resize(image.size());
    }
}
Beispiel #3
0
int main(int argc,char** argv)
	{
	int width;
	int height;
	
	const char* source;		// Source code
	cl_platform_id platform;	// Platform { CPU, GPU, FPGA }
	cl_device_id device;		// Device { GPU_ID }
	cl_context context;		// Context
	cl_command_queue queue;		// Queue 
	cl_int err;			// for error checking

	cl_event event[NR_KERNELS-1];	// must be ok for second kernel to run

	cl_program program;		// program
	cl_kernel kernels_code[NR_KERNELS];


	cl_mem bufferIN;	// data IN
	cl_mem bufferOUT;	// data OUT

	int* inputImage;	// input image
	int* outputImage;	// output image

	cl_uint *ptr;			// data to be shown
	size_t global_work_size;	// work size

	// get platform, get device, create context
	clGetPlatformIDs(1,&platform,NULL);
	clGetDeviceIDs(platform,COMPUTE,1,&device,NULL);
	context=clCreateContext(NULL,1,&device,NULL,NULL,NULL);
	queue=clCreateCommandQueue(context,device,0,NULL);

	
	
	// read data from file, allocate memory & copy
	inputImage=input_image(INPUT_IMAGE,&width,&height);
	
	bufferIN=clCreateBuffer(context,CL_MEM_READ_WRITE,width*height *sizeof(int),NULL,NULL);
	clEnqueueWriteBuffer(queue,bufferIN,1,0,width * height * sizeof(int),inputImage,0,0,0);

	bufferOUT= clCreateBuffer(context,CL_MEM_READ_WRITE,width*height *sizeof(int),NULL,NULL);	

	// read code from file / check code
	source=readCode(FILENAME);
	if(source==NULL)
		{
		printf("Error reading code. Exiting");
		return 1;
		}
	
	// init OPENCL program
	
	
	program=clCreateProgramWithSource(context,1,&source,NULL,&err);
	check(err,program,device);
	check(clBuildProgram(program,1,&device,NULL,NULL,NULL));


	// kernels
	for(int i=0;i<NR_KERNELS;i++)
		{
			kernels_code[i]=clCreateKernel(program,kernels[i],&err);
			check(err,program,device);
		}
	

	// pass args, execute OPENCL kernels
	global_work_size=width*height;

	for(int i=0;i<NR_KERNELS;i++)
	{	
		if((i+1)%2==0){
			check(clSetKernelArg(kernels_code[i],0,sizeof(bufferOUT),(void*)&bufferOUT));
			check(clSetKernelArg(kernels_code[i],1,sizeof(bufferIN),(void*)&bufferIN));
			check(clSetKernelArg(kernels_code[i],2,sizeof(int),(void*)&width));
			check(clSetKernelArg(kernels_code[i],3,sizeof(int),(void*)&height));
			}
		else {
			check(clSetKernelArg(kernels_code[i],0,sizeof(bufferIN),(void*)&bufferIN));
			check(clSetKernelArg(kernels_code[i],1,sizeof(bufferOUT),(void*)&bufferOUT));
			check(clSetKernelArg(kernels_code[i],2,sizeof(int),(void*)&width));
			check(clSetKernelArg(kernels_code[i],3,sizeof(int),(void*)&height));
			}
	}

	

	
	size_t global_work[2];
	global_work[0]=width;
	global_work[1]=height;

	// enqueue / run all kernels (pipeline in this case)

	// start first kernel
	check(clEnqueueNDRangeKernel(queue,kernels_code[0],2,NULL,global_work,NULL,0,NULL,&event[0]));

	// run all the rest (n-2)
	for(int i=1;i<NR_KERNELS-1;i++)
		check(clEnqueueNDRangeKernel(queue,kernels_code[i],2,NULL,global_work,NULL,i,event,&event[i]));

	// start last kernel
	if(NR_KERNELS>2)
	check(clEnqueueNDRangeKernel(queue,kernels_code[NR_KERNELS-1],2,NULL,global_work,NULL,NR_KERNELS-1,event,NULL));
		
	
	// finish queue
	check(clFinish(queue));
	 
	if(NR_KERNELS%2==1)
    		outputImage= (int*)clEnqueueMapBuffer(queue,bufferOUT,CL_TRUE,CL_MAP_READ,0,width*height*sizeof(int),0,NULL,NULL,NULL);
	else 
		outputImage= (int*)clEnqueueMapBuffer(queue,bufferIN,CL_TRUE,CL_MAP_READ,0,width*height*sizeof(int),0,NULL,NULL,NULL);
    	
    	output_image(OUTPUT_IMAGE,outputImage,width,height);
	return 0;
}
int gslic_main(int argc, char *argv[])
{
	try
	{
		cv::theRNG();

#if 0
		if (cv::gpu::getCudaEnabledDeviceCount() > 0)
		{
			std::cout << "GPU info:" << std::endl;
			cv::gpu::printShortCudaDeviceInfo(cv::gpu::getDevice());
		}
		else
			std::cout << "GPU not found ..." << std::endl;
#endif

		{
			std::list<std::string> input_file_list;
#if 1
			input_file_list.push_back("./data/machine_vision/opencv/pic1.png");
			input_file_list.push_back("./data/machine_vision/opencv/pic2.png");
			input_file_list.push_back("./data/machine_vision/opencv/pic3.png");
			input_file_list.push_back("./data/machine_vision/opencv/pic4.png");
			input_file_list.push_back("./data/machine_vision/opencv/pic5.png");
			input_file_list.push_back("./data/machine_vision/opencv/pic6.png");
			input_file_list.push_back("./data/machine_vision/opencv/stuff.jpg");
			input_file_list.push_back("./data/machine_vision/opencv/synthetic_face.png");
			input_file_list.push_back("./data/machine_vision/opencv/puzzle.png");
			input_file_list.push_back("./data/machine_vision/opencv/fruits.jpg");
			input_file_list.push_back("./data/machine_vision/opencv/lena_rgb.bmp");
			input_file_list.push_back("./data/machine_vision/opencv/hand_01.jpg");
			input_file_list.push_back("./data/machine_vision/opencv/hand_05.jpg");
			input_file_list.push_back("./data/machine_vision/opencv/hand_24.jpg");
#elif 0
			input_file_list.push_back("./data/machine_vision/opencv/image_undistortion/kinect_rgba_20130530T103805.png");
			input_file_list.push_back("./data/machine_vision/opencv/image_undistortion/kinect_rgba_20130531T023152.png");
			input_file_list.push_back("./data/machine_vision/opencv/image_undistortion/kinect_rgba_20130531T023346.png");
			input_file_list.push_back("./data/machine_vision/opencv/image_undistortion/kinect_rgba_20130531T023359.png");
#elif 0
			input_file_list.push_back("../../hw_interface/bin/data/kinect/kinect2_rgba_20130725T211659.png");
			input_file_list.push_back("../../hw_interface/bin/data/kinect/kinect2_rgba_20130725T211705.png");
			input_file_list.push_back("../../hw_interface/bin/data/kinect/kinect2_rgba_20130725T211713.png");
			input_file_list.push_back("../../hw_interface/bin/data/kinect/kinect2_rgba_20130725T211839.png");
			input_file_list.push_back("../../hw_interface/bin/data/kinect/kinect2_rgba_20130725T211842.png");
#endif

			//
			const int num_segments = 1200;
			const SEGMETHOD seg_method = XYZ_SLIC;  // SLIC, RGB_SLIC, XYZ_SLIC.
			const double seg_weight = 0.3;

#if 0
			std::vector<cv::Mat> input_images;
			input_images.reserve(input_file_list.size());
			for (std::list<std::string>::iterator it = input_file_list.begin(); it != input_file_list.end(); ++it)
			{
				cv::Mat img(cv::imread(*it, CV_LOAD_IMAGE_COLOR));
				if (img.empty())
				{
					std::cout << "Image file not found: " << *it << std::endl;
					continue;
				}
			}

			local::gslic_sample(input_images, seg_method, seg_weight, num_segments);
#else
			cv::Mat superpixel_mask, superpixel_boundary;
			for (std::list<std::string>::iterator it = input_file_list.begin(); it != input_file_list.end(); ++it)
			{
				cv::Mat input_image(cv::imread(*it, CV_LOAD_IMAGE_COLOR));
				if (input_image.empty())
				{
					std::cout << "Image file not found: " << *it << std::endl;
					continue;
				}

				// Smoothing.
				local::smooth_image(input_image.clone(), input_image);

				// Superpixel.
				{
					const int64 start = cv::getTickCount();

					// Superpixel mask consists of segment indexes.
					my_gslic::create_superpixel_by_gSLIC(input_image, superpixel_mask, seg_method, seg_weight, num_segments);
					my_gslic::create_superpixel_boundary(superpixel_mask, superpixel_boundary);

					const int64 elapsed = cv::getTickCount() - start;
					const double freq = cv::getTickFrequency();
					const double etime = elapsed * 1000.0 / freq;
					const double fps = freq / elapsed;
					std::cout << std::setprecision(4) << "elapsed time: " << etime <<  ", FPS: " << fps << std::endl;
				}

#if 0
				// Show superpixel mask.
				cv::Mat mask;
				double minVal = 0.0, maxVal = 0.0;
				cv::minMaxLoc(superpixel_mask, &minVal, &maxVal);
				superpixel_mask.convertTo(mask, CV_32FC1, 1.0 / maxVal, 0.0);

				cv::imshow("Superpixels by gSLIC - mask", mask);
#endif

#if 1
				// Show superpixel boundary.
				//cv::Mat superpixel_boundary;
				//my_gslic::create_superpixel_boundary(superpixel_mask, superpixel_boundary);

				cv::Mat img(input_image.clone());
				img.setTo(cv::Scalar(0, 0, 255), superpixel_boundary);

				cv::imshow("Superpixels by gSLIC - boundary", img);
#endif

				const unsigned char key = cv::waitKey(0);
				if (27 == key)
					break;
			}
#endif

			cv::destroyAllWindows();
		}
	}
	catch (const cv::Exception &e)
	{
		//std::cout << "OpenCV exception caught: " << e.what() << std::endl;
		//std::cout << "OpenCV exception caught: " << cvErrorStr(e.code) << std::endl;
		std::cout << "OpenCV exception caught:" << std::endl
			<< "\tdescription: " << e.err << std::endl
			<< "\tline:        " << e.line << std::endl
			<< "\tfunction:    " << e.func << std::endl
			<< "\tfile:        " << e.file << std::endl;

		return 1;
	}

	return 0;
}