问题
I m trying to find the maximum and minimum of RGB values of an image. the flow in which i was planning to go is:
- load the image.
- after loading the image, create a 15x15 cell around the cell to be tested
- find the max of RGB of the test cell and store it in an array.
- then print the image with the value of max RGB, According to me the image should be a DARK image. The max of RGB corresponds to the dark portion of the image
The problem here is i m new to image processing, opencv. i dont know how to implement these things which i mentioned abovei have attached a picture related to my doubt
Here is code, i have just read image and got some details of image
#include "iostream"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "opencv2/opencv.hpp"
float lambda=0.0001; //lambda
double _w=0.95; //w
int height=0; //image Height
int width=0; //image Width
int size=0; //total number of pixels
int blockdim = 32;
char img_name[100]="1.png";
Mat read_image()
{
Mat img = imread(img_name);
height = img.rows;
width = img.cols;
size = img.rows*img.cols;
Mat real_img(img.rows,img.cols,CV_32FC3);
img.convertTo(real_img,CV_32FC3);
return real_img;
}
//Main Function
int main(int argc, char * argv[])
{
Mat img = read_image();
/*****************************************************************/
// Till here i have done my code. i.e. Read my image and get all details about the image
// Now i'm not getting the logic to find the Min/Max of RGB values in an image for
// 15x15 cell
return 0;
}
Finally i want to implement this on GPU, i have learnt few things about GPU,CUDA and played on GPU. Now i wanna do some stuff related to image processing on GPU(CUDA)
I want to compute the extent of haze of an image for each block. This is done by finding the dark channel value that is used to reflect the extent of haze. This concept is from Kaiming He's paper on a Single Image Haze Removal using Dark Channel Prior.
The dark channel value for each block is defined as follows:
where I^c (x',y') denotes the intensity at a pixel location (x',y') in color channel c (one of Red, Green, or Blue color channel), and omega(x,y) denotes the neighborhood of the pixel location (x',y').
since i m new to Image processing and open cv, i'm not sure how to translate this equation
回答1:
I already implemented this some time ago, and below is the code snippet. Probably could be further optimized, and you should add cuda support by yourself, but this could be a good starting point.
The main steps are:
- Load a BGR image
- Compute a single channel matrix with the minimum of B,G,R (
minValue3b). - Compute the minimum in a
patchSize x patchSizeneighborhood (minFilter).
NOTES
- You need to find the minimum value, not the maximum.
- To avoid border issues while searching the minimum in the neighborhood, you can simply add a border big enough around the image, with the maximum allowed value (i.e. 255). You can use
copyMakeBorderfor this.
Input:
DCP:
Code:
#include <opencv2/opencv.hpp>
using namespace cv;
void minFilter(const Mat1b& src, Mat1b& dst, int radius)
{
Mat1b padded;
copyMakeBorder(src, padded, radius, radius, radius, radius, BORDER_CONSTANT, Scalar(255));
int rr = src.rows;
int cc = src.cols;
dst = Mat1b(rr, cc, uchar(0));
for (int c = 0; c < cc; ++c)
{
for (int r = 0; r < rr; ++r)
{
uchar lowest = 255;
for (int i = -radius; i <= radius; ++i)
{
for (int j = -radius; j <= radius; ++j)
{
uchar val = padded(radius + r + i, radius + c + j);
if (val < lowest) lowest = val;
}
}
dst(r, c) = lowest;
}
}
}
void minValue3b(const Mat3b& src, Mat1b& dst)
{
int rr = src.rows;
int cc = src.cols;
dst = Mat1b(rr, cc, uchar(0));
for (int c = 0; c<cc; ++c)
{
for (int r = 0; r<rr; ++r)
{
const Vec3b& v = src(r, c);
uchar lowest = v[0];
if (v[1] < lowest) lowest = v[1];
if (v[2] < lowest) lowest = v[2];
dst(r, c) = lowest;
}
}
}
void DarkChannel(const Mat3b& img, Mat1b& dark, int patchSize)
{
int radius = patchSize / 2;
Mat1b low;
minValue3b(img, low);
minFilter(low, dark, radius);
}
int main()
{
// Load the image
Mat3b img = imread("path_to_image");
// Compute DCP
Mat1b dark;
DarkChannel(img, dark, 15);
// Show results
imshow("Img", img);
imshow("Dark", dark);
waitKey();
return 0;
}
来源:https://stackoverflow.com/questions/33252686/how-to-find-haze-extent-of-an-image-with-opencv-on-cuda