[問題]opencv calcHist計算與畫直方圖的問題
開發平台(Platform): (Ex: Win10, Linux, ...)
win7
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
c++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
opencv
問題(Question):
在使用calcHist做直方圖的計算時以整張圖來做計算及畫圖是OK的
但想要更精確表示圖像的話必須將圖塊切塊後(EX:3*3)再將各區塊的直方圖連接起來
做為整張圖的特徵直方圖 我用了阿洲的程式教學裡的方式做運算
但只能做單一整張的直方圖 不知有無方式可以繪製出連接後的直方圖呢
謝謝大家
餵入的資料(Input):
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <cstdio>
#include <opencv2/opencv.hpp>
using namespace cv;
void drawHistImg(const Mat &src, Mat &dst);
int main(){
Mat src = imread("lena.jpg",CV_LOAD_IMAGE_GRAYSCALE);
int histSize = 256;
float range[] = {0, 255} ;
const float* histRange = {range};
Mat histImg;
calcHist(&src, 1, 0, Mat(), histImg, 1, &histSize, &histRange);
Mat showHistImg(256,256,CV_8UC1,Scalar(255)); //把直方圖秀在一個256*256大
的影像上
drawHistImg(histImg, showHistImg);
imshow("window1", src);
imshow("window2", showHistImg);
waitKey(0);
return 0;
}
void drawHistImg(const Mat &src, Mat &dst){
int histSize = 256;
float histMaxValue = 0;
for(int i=0; i<histSize; i++){
float tempValue = src.at<float>(i);
if(histMaxValue < tempValue){
histMaxValue = tempValue;
}
}
float scale = (0.9*256)/histMaxValue;
for(int i=0; i<histSize; i++){
int intensity = static_cast<int>(src.at<float>(i)*scale);
line(dst,Point(i,255),Point(i,255-intensity),Scalar(0));
}
}
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.116.40.159
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1501145128.A.545.html
→
07/28 21:57, , 1F
07/28 21:57, 1F
→
07/28 21:58, , 2F
07/28 21:58, 2F
→
07/29 09:44, , 3F
07/29 09:44, 3F
→
07/29 09:44, , 4F
07/29 09:44, 4F
推
07/29 13:10, , 5F
07/29 13:10, 5F
→
07/30 14:10, , 6F
07/30 14:10, 6F
→
07/30 15:49, , 7F
07/30 15:49, 7F
推
07/31 06:52, , 8F
07/31 06:52, 8F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章