報錯

代碼:
#include <stdio.h>
#include <string>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
// 大均法函式實作
int OTSU(cv::Mat srcImage)
{
int nCols = srcImage.cols;
int nRows = srcImage.rows;
int threshold = 0;
// 初始化統計引數
int nSumPix[256];
float nProDis[256];
for (int i = 0; i < 256; i++)
{
nSumPix[i] = 0;
nProDis[i] = 0;
}
// 統計灰度級中每個像素在整幅影像中的個數
for (int i = 0; i < nCols; i++)
{
for (int j = 0; j < nRows; j++)
{
nSumPix[(int)srcImage.at<uchar>(i, j)]++;
}
}
// 計算每個灰度級占影像中的概率分布
for (int i = 0; i < 256; i++)
{
nProDis[i] = (float)nSumPix[i] / (nCols * nRows);
}
// 遍歷灰度級[0,255],計算出最大類間方差下的閾值
float w0, w1, u0_temp, u1_temp, u0, u1, delta_temp;
double delta_max = 0.0;
for (int i = 0; i < 256; i++)
{
// 初始化相關引數
w0 = w1 = u0_temp = u1_temp = u0 = u1 = delta_temp = 0;
for (int j = 0; j < 256; j++)
{
//背景部分
if (j <= i)
{
// 當前i為分割閾值,第一類總的概率
w0 += nProDis[j];
u0_temp += j * nProDis[j];
}
//前景部分
else
{
// 當前i為分割閾值,第一類總的概率
w1 += nProDis[j];
u1_temp += j * nProDis[j];
}
}
// 分別計算各類的平均灰度
u0 = u0_temp / w0;
u1 = u1_temp / w1;
delta_temp = (float)(w0 *w1* pow((u0 - u1), 2));
// 依次找到最大類間方差下的閾值
if (delta_temp > delta_max)
{
delta_max = delta_temp;
threshold = i;
}
}
return threshold;
}
int main()
{
// 影像讀取及判斷
//cv::Mat srcImage = cv::imread("2.jpg");
//if (!srcImage.data)
// return 1;
// 灰度轉換
VideoCapture cap("walk.avi");
while(1){
Mat frame;
cap>>frame;
cv::Mat srcGray;
cv::cvtColor(frame, srcGray, CV_RGB2GRAY);
cv::imshow("srcGray", srcGray);
// 呼叫OTSU二值化演算法得到閾值
if(!srcGray.empty())
{
int ostuThreshold = OTSU(srcGray);
std::cout << ostuThreshold << std::endl;
// 定義輸出結果影像
cv::Mat otsuResultImage =
cv::Mat::zeros(srcGray.rows, srcGray.cols, CV_8UC1);
// 利用得到的閾值實作二值化操作
for (int i = 0; i < srcGray.rows; i++)
{
for (int j = 0; j < srcGray.cols; j++)
{
// 滿足大于閾值ostuThreshold置255
if (srcGray.at<uchar>(i, j) > ostuThreshold)
otsuResultImage.at<uchar>(i, j) = 255;
else
otsuResultImage.at<uchar>(i, j) = 0;
}
}
cv::imshow("otsuResultImage", otsuResultImage);
cv::waitKey(0);}}
return 0;
}
uj5u.com熱心網友回復:
崩潰的時候在彈出的對話框按相應按鈕進入除錯,按Alt+7鍵查看Call Stack即“呼叫堆疊”里面從上到下列出的對應從里層到外層的函式呼叫歷史。雙擊某一行可將游標定位到此次呼叫的源代碼或匯編指令處,看不懂時雙擊下一行,直到能看懂為止。uj5u.com熱心網友回復:
請仔細檢查一下影像和演算法的引數限制uj5u.com熱心網友回復:
資料型別的問題,先搞清楚你的視頻是什么格式得吧。先把cv::imshow("srcGray", srcGray);以下的全注釋掉,看看還有沒這錯uj5u.com熱心網友回復:
非回答欄:轉化為代碼塊 看著舒服點#include <stdio.h>
#include <string>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
// 大均法函式實作
int OTSU(cv::Mat srcImage)
{
int nCols = srcImage.cols;
int nRows = srcImage.rows;
int threshold = 0;
// 初始化統計引數
int nSumPix[256];
float nProDis[256];
for (int i = 0; i < 256; i++)
{
nSumPix[i] = 0;
nProDis[i] = 0;
}
// 統計灰度級中每個像素在整幅影像中的個數
for (int i = 0; i < nCols; i++)
{
for (int j = 0; j < nRows; j++)
{
nSumPix[(int)srcImage.at<uchar>(i, j)]++;
}
}
// 計算每個灰度級占影像中的概率分布
for (int i = 0; i < 256; i++)
{
nProDis[i] = (float)nSumPix[i] / (nCols * nRows);
}
// 遍歷灰度級[0,255],計算出最大類間方差下的閾值
float w0, w1, u0_temp, u1_temp, u0, u1, delta_temp;
double delta_max = 0.0;
for (int i = 0; i < 256; i++)
{
// 初始化相關引數
w0 = w1 = u0_temp = u1_temp = u0 = u1 = delta_temp = 0;
for (int j = 0; j < 256; j++)
{
//背景部分
if (j <= i)
{
// 當前i為分割閾值,第一類總的概率
w0 += nProDis[j];
u0_temp += j * nProDis[j];
}
//前景部分
else
{
// 當前i為分割閾值,第一類總的概率
w1 += nProDis[j];
u1_temp += j * nProDis[j];
}
}
// 分別計算各類的平均灰度
u0 = u0_temp / w0;
u1 = u1_temp / w1;
delta_temp = (float)(w0 *w1* pow((u0 - u1), 2));
// 依次找到最大類間方差下的閾值
if (delta_temp > delta_max)
{
delta_max = delta_temp;
threshold = i;
}
}
return threshold;
}
int main()
{
// 影像讀取及判斷
//cv::Mat srcImage = cv::imread("2.jpg");
//if (!srcImage.data)
// return 1;
// 灰度轉換
VideoCapture cap("walk.avi");
while(1){
Mat frame;
cap>>frame;
cv::Mat srcGray;
cv::cvtColor(frame, srcGray, CV_RGB2GRAY);
cv::imshow("srcGray", srcGray);
// 呼叫OTSU二值化演算法得到閾值
if(!srcGray.empty())
{
int ostuThreshold = OTSU(srcGray);
std::cout << ostuThreshold << std::endl;
// 定義輸出結果影像
cv::Mat otsuResultImage =
cv::Mat::zeros(srcGray.rows, srcGray.cols, CV_8UC1);
// 利用得到的閾值實作二值化操作
for (int i = 0; i < srcGray.rows; i++)
{
for (int j = 0; j < srcGray.cols; j++)
{
// 滿足大于閾值ostuThreshold置255
if (srcGray.at<uchar>(i, j) > ostuThreshold)
otsuResultImage.at<uchar>(i, j) = 255;
else
otsuResultImage.at<uchar>(i, j) = 0;
}
}
cv::imshow("otsuResultImage", otsuResultImage);
cv::waitKey(0);}}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/98302.html
標籤:圖形處理/算法
