40 providerName = getProperty<std::string>(
"providerName").getValue();
41 usingImageProvider(providerName);
43 offeringTopic(getProperty<std::string>(
"DebugObserverName").getValue());
44 frameRate = getProperty<float>(
"Framerate").getValue();
45 thresholdLaplace = getProperty<float>(
"ThresholdLaplaceBlur").getValue();
46 thresholdPerceptual = getProperty<float>(
"ThresholdPerceptualBlur").getValue();
51 std::unique_lock lock(imageMutex);
54 imageProviderPrx = getProxy<visionx::ImageProviderInterfacePrx>(providerName);
56 cameraImages =
new CByteImage*[2];
61 debugObserver = getTopic<DebugObserverInterfacePrx>(getProperty<std::string>(
"DebugObserverName").getValue());
75 std::unique_lock lock(imageMutex);
77 if (!waitForImages(getProperty<std::string>(
"providerName").getValue(), 1000))
79 ARMARX_WARNING <<
"Timeout while waiting for camera images (>1000ms)";
83 int numImages = getImages(cameraImages);
93 IplImage* ppIplImages[1] = { IplImageAdaptor::Adapt(cameraImages[0]) };
95 cv::Mat image = cv::cvarrToMat(ppIplImages[0]);
98 cv::cvtColor(image, gray_image, cv::COLOR_BGR2GRAY);
100 double blurrinessLaplaceVariance = laplaceVarianceBlurrinessMetric(gray_image);
101 double blurrinessPerceptualBlur = blurringPerceptual(gray_image);
102 double blurrinessMarziliano = blurringMarziliano(gray_image);
104 ARMARX_LOG <<
deactivateSpam(1) <<
"blurrinessLaplaceVariance: " << ((blurrinessLaplaceVariance < thresholdLaplace) ?
"Blurry" :
"Not blurry") <<
" Value: " << blurrinessLaplaceVariance;
105 ARMARX_LOG <<
deactivateSpam(1) <<
"blurrinessPerceptialBlur: " << ((blurrinessPerceptualBlur > thresholdPerceptual) ?
"Blurry" :
"Not blurry") <<
" Value: " << blurrinessPerceptualBlur;
109 debugValues[
"BlurrinessValueLaplace"] =
new Variant(blurrinessLaplaceVariance);
110 debugValues[
"BlurryLaplace"] =
new Variant((blurrinessLaplaceVariance < thresholdLaplace));
112 debugValues[
"BlurrinessValuePerceptual"] =
new Variant(blurrinessPerceptualBlur);
113 debugValues[
"BlurryPerceptual"] =
new Variant((blurrinessPerceptualBlur > thresholdPerceptual));
115 debugValues[
"BlurrinessValueMarziliano"] =
new Variant(blurrinessMarziliano);
117 debugValues[
"seq"] =
new Variant((
int) seq);
120 debugObserver->setDebugChannel(
"BlurrinessMetric", debugValues);
122 CByteImage* resultImages[1] = { IplImageAdaptor::Adapt(ppIplImages[0]) };
123 provideResultImages(resultImages);
127 fpsCounter.assureFPS(frameRate);
139 double armarx::BlurrinessMetric::laplaceVarianceBlurrinessMetric(cv::Mat& in)
142 cv::Laplacian(in, out, CV_64F);
145 cv::meanStdDev(out,
mean, variance);
147 return (variance.val[0] * variance.val[0]);
163 double armarx::BlurrinessMetric::blurringPerceptual(
const cv::Mat& src)
165 double blur_index = 0;
167 cv::Mat smoothV(src.rows, src.cols, CV_8UC1);
168 cv::Mat smoothH(src.rows, src.cols, CV_8UC1);
169 cv::blur(src, smoothV, cv::Size(1, 9));
170 cv::blur(src, smoothH, cv::Size(9, 1));
173 double difS_V = 0, difS_H = 0, difB_V = 0, difB_H = 0;
174 double somaV = 0, somaH = 0, varV = 0, varH = 0;
176 for (
int i = 0; i < src.rows; ++i)
179 for (
int j = 0; j < src.cols; ++j)
184 difS_V =
abs(src.at<uchar>(i, j) - src.at<uchar>(i - 1, j));
185 difB_V =
abs(smoothV.at<uchar>(i, j) - smoothV.at<uchar>(i - 1, j));
190 difS_H =
abs(src.at<uchar>(i, j) - src.at<uchar>(i, j - 1));
191 difB_H =
abs(smoothH.at<uchar>(i, j) - smoothH.at<uchar>(i, j - 1));
194 varV +=
cv::max(0.0, difS_V - difB_V);
195 varH +=
cv::max(0.0, difS_H - difB_H);
201 blur_index =
cv::max((somaV - varV) / somaV, (somaH - varH) / somaH);
218 double armarx::BlurrinessMetric::blurringMarziliano(
const cv::Mat& src)
220 double blur_index = 0;
222 cv::Mat edges(src.rows, src.cols, CV_8UC1);
223 cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
225 cv::GaussianBlur(src, edges, cv::Size(3, 3), 0);
226 cv::morphologyEx(edges, edges, cv::MORPH_OPEN, element);
227 cv::morphologyEx(edges, edges, cv::MORPH_CLOSE, element);
228 cv::Sobel(edges, edges, CV_8UC1, 1, 0);
230 unsigned int edge_counter = 0;
240 for (
int i = 0; i < src.rows; ++i)
242 for (
int j = 0; j < src.cols; ++j)
248 if (edges.at<uchar>(i, j) > 0)
260 if ((src.at<uchar>(i, j - 1) - src.at<uchar>(i, j)) > 0)
263 max = src.at<uchar>(i, k);
264 while ((
max <= src.at<uchar>(i, k - 1)) && (k > 1))
267 max = src.at<uchar>(i, k);
274 min = src.at<uchar>(i, k);
275 while ((
min >= src.at<uchar>(i, k - 1)) && (k > 1))
278 min = src.at<uchar>(i, k);
285 if (j == (src.cols - 1))
287 c_end = src.cols - 1;
292 if ((src.at<uchar>(i, j + 1) - src.at<uchar>(i, j)) > 0)
295 max = src.at<uchar>(i, k);
296 while (
max <= src.at<uchar>(i, k + 1))
299 max = src.at<uchar>(i, k);
301 if (k == src.cols - 1)
310 min = src.at<uchar>(i, k);
311 while (
min >= src.at<uchar>(i, k + 1))
314 min = src.at<uchar>(i, k);
316 if (k == src.cols - 1)
324 assert((c_end - c_start) >= 0);
325 length += (c_end - c_start);
331 if (edge_counter != 0)
333 blur_index = ((double) length) / edge_counter;