C++ 在OpenCV上使用原始版本和平滑版本之间的色调比较对图像进行排序:这足够了吗?

C++ 在OpenCV上使用原始版本和平滑版本之间的色调比较对图像进行排序:这足够了吗?,c++,opencv,image-processing,C++,Opencv,Image Processing,问题是如何将图片与其他类型的图片进行排序。在另一个例子中,我尝试利用提供的使用ImageMagick编写的脚本。我现在正在探索同样的问题,使用OpenCV 如解决方案中所述,想法是将颜色模式更改为HSV,即图像,并将其频道与原始频道进行比较;然后把它们区别开来,因为所谓的漫画式图像与现实生活中的图像不同,因为它们对操作的反应不同。所以我试着: #include "imgproc/imgproc.hpp" #include "highgui/highgui.hpp" #include "cv.h"

问题是如何将图片与其他类型的图片进行排序。在另一个例子中,我尝试利用提供的使用ImageMagick编写的脚本。我现在正在探索同样的问题,使用OpenCV

如解决方案中所述,想法是将颜色模式更改为HSV,即图像,并将其频道与原始频道进行比较;然后把它们区别开来,因为所谓的漫画式图像与现实生活中的图像不同,因为它们对操作的反应不同。所以我试着:

#include "imgproc/imgproc.hpp"
#include "highgui/highgui.hpp"
#include "cv.h"  
#include <iostream>   
#include <stdio.h>  

using namespace std;
using namespace cv;

int main(int argc, char *argv[]) {

//takes filename as argument i.e. ./smooth one.jpg  
    cv::Mat input, output, hsv;
    input = imread(argv[1],1);

//Resize to 300
    Size size(300,300);
    resize(input,input,size);

//Change original image color mode to HSV
    cvtColor(input,hsv,CV_RGB2HSV);

//Separate channels; keep hue on h_image
    std::vector<cv::Mat> hsv_channels, hsv_channelsOUT;
    cv::split(hsv, hsv_channels);
    cv::Mat h_image = hsv_channels[0];

//Apply smoothing/blur on h_image, to output        
    bilateralFilter(h_image,output,60,120,30);

//Split output(already just H) to extract H array for mean later
    cv::split(output, hsv_channelsOUT);
    cv::Mat O_imageH = hsv_channelsOUT[0];

//Average all differences between H values pre/post blur
//Compare h_image with h_image+bilinearF visually to see
    Scalar h = mean(hsv_channels[0]-hsv_channelsOUT[0]);

//Print that to console 
    printf("H: %f\n", h[0]);

//Showcase all the steps; comment to use to binary in scripts
//All windows piled up on top of another one at runtime; move to see
cv::imshow("input",input);
cv::imshow("hsv",hsv);
cv::imshow("h_image",h_image);
cv::imshow("output",output);
cv::imshow("h_image+bilinerarF",O_imageH);

//exit on keypress on image displayed
    waitKey();   
    return 0;   
  }
我真的不知道如何设置双边过滤器,因为我不知道什么是sigma空间等。我最初将其设置为OpenCV文档中生成的最大值示例请参见简介。我将值加倍,因为这扩大了某些类型图像之间H值的差异。我认为这是它们的范围。白色上的所有黑线艺术的H值都将为0——这只是所做工作的副作用,它们的色调通道都是黑色的。因此,这些可以很容易地归类为这样。我没有足够的图像集来进行预测并相应地调整双边滤波器。我似乎有H:4-5的相机图像;7-9; 我得到的最小值0.08是从蓝色到绿色的相同非不透明三角形图形图案;所有这些都是对太小样本的观察,无法推断出任何有意义的东西

问:那么我想知道,我是否根据我基于此的理论正确地实现了这个想法?我是否应该得出结论,需要一个更具弹性的程序,使用许多特性来完成我想做的事情?我应该以不同的方式设置双边过滤器吗?

这在某种程度上起作用。现在还不清楚如何调整这个。但我未能正确地组装元素,以使用ImageMagick创建索引。我也有一些问题,脚本用于粘合在一起,但提供了一些。所以我能让它正常工作:

#!/usr/bin/env sh

##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## -=Try to sort images based on content (helper script)=-
## re: http://stackoverflow.com/q/26951796
## We will generate values for each jpg with our OpenCV thing, sort, then index
## accordingly with IM.
## Our challenge is that we cannot supply 307 jpgs at a time for final montage
## So we'll split that in rows of 15 images.
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

##OpenCV## find all files in current dir/subdirs, run our OpenCV binary
# which echoes the name of the file: H: value #################################

find . -type f -name '*.[Jj][[Pp][Gg]' -exec sh -c '
    for file do
    echo -n "$file: "
    ./smooth "$file"   #name of our OpenCV binary in local dir only
done
' sh {} + | sort --version-sort -k3 | tee Hout | cut -d ':' -f 1,1 > Hpaths_sorted

##ImageMagick - "montage" can't make an index with 100s of images at 100x100
# on my rig; so horizontal strips of 15 images; we will have $z many strips
# i.e. ceiling for not fully populated row ############################

a="1"
h="15"
j="$(cat Hpaths_sorted | wc -l)"
z="$(echo "a=$j; b=$h; if ( a%b ) a/b+1 else a/b" | bc)"

for i in $(seq 1 "$z"); do 

    strip=$(awk "NR>=$a&&NR<=$h" Hpaths_sorted)
    if [ "$h" -gt "$j" ]; then
        montage $(echo "$strip"| tr '\n' ' ') -tile x1 -geometry 100x100+0+0 $i.gif
    else
        montage $(echo "$strip"| tr '\n' ' ') -tile 15x -geometry 100x100+0+0 $i.gif
        a="$(echo $(($a+15)))"
        h="$(echo $(($h+15)))"
    fi

    target="$target $i.gif"

done

# Use our $target to supply the list of files(strips), make one index, 1 row x #strips to final.gif

montage $target -tile 1x$z -geometry '1x1+0+0<' final.gif
在我们的358图像集上产生:


低值在顶部,我可以看到一些分组,我们已经正确地生成了不完整的行。因此,它在其最简单的形式中可能没有那么有用,但它确实可以被使用,因为它值得探索……

色调通道的平均值对于比较图像来说是一个糟糕的度量,而且任何类型的平滑都不会有任何效果,imho。相反,尝试从色调通道绘制直方图,并进行比较。再说一次,请更明确一点,关于你想要达到的目标。啊,等等,我可能误解了你的问题。@berak我做了一个编辑,让这个更清楚。再次感谢。
#!/usr/bin/env sh

##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## -=Try to sort images based on content (helper script)=-
## re: http://stackoverflow.com/q/26951796
## We will generate values for each jpg with our OpenCV thing, sort, then index
## accordingly with IM.
## Our challenge is that we cannot supply 307 jpgs at a time for final montage
## So we'll split that in rows of 15 images.
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

##OpenCV## find all files in current dir/subdirs, run our OpenCV binary
# which echoes the name of the file: H: value #################################

find . -type f -name '*.[Jj][[Pp][Gg]' -exec sh -c '
    for file do
    echo -n "$file: "
    ./smooth "$file"   #name of our OpenCV binary in local dir only
done
' sh {} + | sort --version-sort -k3 | tee Hout | cut -d ':' -f 1,1 > Hpaths_sorted

##ImageMagick - "montage" can't make an index with 100s of images at 100x100
# on my rig; so horizontal strips of 15 images; we will have $z many strips
# i.e. ceiling for not fully populated row ############################

a="1"
h="15"
j="$(cat Hpaths_sorted | wc -l)"
z="$(echo "a=$j; b=$h; if ( a%b ) a/b+1 else a/b" | bc)"

for i in $(seq 1 "$z"); do 

    strip=$(awk "NR>=$a&&NR<=$h" Hpaths_sorted)
    if [ "$h" -gt "$j" ]; then
        montage $(echo "$strip"| tr '\n' ' ') -tile x1 -geometry 100x100+0+0 $i.gif
    else
        montage $(echo "$strip"| tr '\n' ' ') -tile 15x -geometry 100x100+0+0 $i.gif
        a="$(echo $(($a+15)))"
        h="$(echo $(($h+15)))"
    fi

    target="$target $i.gif"

done

# Use our $target to supply the list of files(strips), make one index, 1 row x #strips to final.gif

montage $target -tile 1x$z -geometry '1x1+0+0<' final.gif