作者tyc5116 (累人啊....)
看板C_and_CPP
標題[問題] OpenCV結合VS元件
時間Mon Nov 26 17:09:49 2012
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
OpenCV
問題(Question):
Data怪怪的
程式碼(Code):(請善用置底文網頁, 記得排版)
簡化一下架構,如下
class ImgProc{
public:
...
private:
IplImage* Image;
//彩色圖
IplImage* Image_Gray;
//灰階圖
IplImage* Image_Handle;
//目前作完影像處理的圖
cv::Mat MatImage;
//對應圖像的矩陣,以下皆是
cv::Mat MatImageGray;
cv::Mat MatHandleImage;
...
};
要將讀進來的圖示透過OpenCV函式,顯示在PictureBox上
(圖的大小為281*500,存檔時存成BMP 24bit)
首先先執行到兩行程式碼
this->Image = cvLoadImage(this->FileName.c_str(),CV_LOAD_IMAGE_COLOR);
this->Image_Gray = cvLoadImage(
this->FileName.c_str(),CV_LOAD_IMAGE_GRAYSCALE);
將讀的檔案分別存成彩色和灰階
google到的作法(只要注意標色的部份即可)
this->ImgObject->SetImgFileName(this->m_ofdOpenFileDia->FileName);
string FileName;
MarshalString(this->ImgObject->GetImgFileName(),FileName);
IplImage *image1 = cvLoadImage(FileName.c_str());
System::Drawing::Bitmap^ Img =
gcnew System::Drawing::Bitmap(
this->ImgObject->GetImage()->width,
//281
this->ImgObject->GetImage()->height,
//500
this->ImgObject->GetImage()->widthStep,
//844
System::Drawing::Imaging::PixelForm[Bat::Format24bppRgb,
(System::IntPtr)this->ImgObject->GetImage()->imageData
);
this->m_PicDetect->Image = Img;
顯示灰階圖的時候,將標色的部份改成
System::Drawing::Imaging::PixelFormat::Format8bppIndexed,
再來我不懂的是,這樣顯示會有問題,還要再加上
Drawing::Imaging::ColorPalette ^palette = Img->Palette;
for( int i=0; i<256; i++)
palette->Entries[i] = Color::FromArgb(i,i,i);
Img->Palette = palette;
this->m_PicDetect->Image = Img;
不懂為何一定要再加上這一段,結果才會正常
(灰階的width,height,widthStep分別為281,500,284)
以及開始作影像處理時(以Smooth為例)
cv::Mat MSmooth(this->ImgObject->GetGrayImageM());
CvMat src = this->ImgObject->GetGrayImageM();
CvMat dsc = MSmooth;
cvSmooth(&src,&dsc,CV_GAUSSIAN);
this->ImgObject->SetHandleImageM(MSmooth);
//cv::imshow("My Window",this->ImgObject->GetHandleImageM());
//這一行結果是正常的
該函式內容為
void ImgProc::SetHandleImageM(cv::Mat Mat){
this->MatHandleImage = Mat;
if (this->Image_Handle != NULL || this->Image_Handle!=0){
delete this->Image_Handle;
this->Image_Handle = new IplImage(Mat);
}
else
this->Image_Handle = new IplImage(Mat);
}
System::Drawing::Bitmap^ Img =
gcnew System::Drawing::Bitmap(
this->ImgObject->GetHandleImage()->width,
//281
this->ImgObject->GetHandleImage()->height,
//500
this->ImgObject->GetHandleImage()->widthStep,
//281,變了
System::Drawing::Imaging::PixelFormat::Format8bppIndexed,
(System::IntPtr)this->ImgObject->GetHandleImage()->imageData
);
看起來是widthStep值變了,造成錯誤,請問值為什麼會作變化?
以上兩個問題,再麻煩解答一下,謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
※ 編輯: tyc5116 來自: 220.134.238.4 (11/26 17:18)
→ diabloevagto:在轉換上本來就是很不可思議了 11/28 01:46
→ diabloevagto:#1FeLuyuO 我有問過不知道跟你的一樣嗎 11/28 01:47
→ diabloevagto:最後我還是放棄了,你可以在繼續嘗試看看 11/28 01:47
→ tyc5116:其實我不是很懂你那個問題,也不知道和這個有什麼關聯,哈~ 11/28 21:22