天天看點

OpenCV&Qt學習之三——圖像的初步處理

來實作适應圖像顯示的,但是由于視窗固定,在打開的圖像小于控件大小時就會縮在左上角顯示,在打開圖像過大時則顯示不全。是以這個例程中首先實作圖像适合視窗的縮放顯示。

由于是基于OpenCV和Qt的圖像處理,是以圖像的縮放處理在OpenCV和Qt都可以完成,我這裡就把OpenCV用作圖像的原始處理,Qt用作顯示處理,是以縮放顯示由Qt完成。

Qt中QImage提供了用于縮放的基本函數,而且功能非常強大,使用Qt自帶的幫助可以檢索到相關資訊。

函數原型:

這是直接擷取大小,還有另一種形式:

函數說明以及參數在文檔中已經說的非常清楚了,文檔摘錄如下:

Returns a copy of the image scaled to a rectangle defined by the given size according to the given aspectRatioModeand transformMode.
OpenCV&Qt學習之三——圖像的初步處理

官方文檔中已經說的比較清楚了,代碼實作也比較簡單,代碼如下:

顯示效果如下:

OpenCV&Qt學習之三——圖像的初步處理

對于以上代碼通過和我之前的代碼做簡單對比,發現有幾點不一樣的地方:

圖像的定義方式,這裡的定義方式為QImage* imgScale = new QImage

scaled函數的調用方式,一個是imgScaled = img.scaled後者為*imgScaled=img->scaled,我最開始也是将.寫為->一直沒找出錯誤,提示base operand of '->' has non-pointer type 'QImage'

繼續查找Qt的幫助手冊,發現QImage的構造函數還真是多:

淩亂查了查資料,網上的資料就那幾個,互相轉來轉去的,而且多數比較老,仍然沒有幫助我想通關于這裡面資料結構的一些疑問,Qt 和 OpenCV對C和指針的要求還是比較高的,長時間從單片機類的程式過來那點功底還真不夠,具體的C應用都忘光了。這個問題隻能暫時擱置,在後面的學習中慢慢了解。

關于圖像資料的基礎知識參見這段介紹:

Fundamentally, an image is a matrix of numerical values. This is why OpenCV 2 manipulates them using the cv::Mat data structure. Each element of the matrix represents one pixel. For a gray-level image (a "black-and-white" image), pixels are unsigned 8-bit values where 0 corresponds to black and corresponds 255 to white. For a color image, three such values per pixel are required to represent the usual three primary color channels {Red, Green, Blue}. A matrix element is therefore made, in this case, of a triplet of values.

這兒以想圖像中添加saltand-pepper noise為例,來說明如何通路圖像矩陣中的獨立元素。saltand-pepper noise就是圖檔中一些像素點,随機的被黑色或者白色的像素點所替代,是以添加saltand-pepper noise也比較簡單,隻需要随機的産生行和列,将這些行列值對應的像素值更改即可,當然通過上面的介紹,需要更改RGB3個通道。程式如下:

<a></a>

對Win 7系統中的自帶圖像考拉進行處理後的效果如下圖所示(程式是Ubuntu 12.04下的):

OpenCV&amp;Qt學習之三——圖像的初步處理

在很多進行中需要對圖檔中的所有像素進行周遊操作,采用什麼方式進行這個操作是需要思考的問題,關于這個問題的論述可以參考下面一段簡介:

Color images are composed of 3-channel pixels. Each of these channels corresponds to the intensity value of one of the three primary colors (red, green, blue). Since each of these values is an 8-bit unsigned char, the total number of colors is 256x256x256, which is more than 16 million colors. Consequently, to reduce the complexity of an analysis, it is sometimes useful to reduce the number of colors in an image. One simple way to achieve this goal is to simply subdivide the RGB space into cubes of equal sizes. For example, if you reduce the number of colors in each dimension by 8, then you would obtain a total of 32x32x32 colors. Each color in the original image is then assigned a new color value in the color-reduced image that corresponds to the value in the center of the cube to which it belongs.

這個例子就是通過操作每一個像素點來減少色彩的位數,基本内容在以上的英文引文中已經有了介紹,代碼的實作也比較直接。在彩色圖像中,3個通道的資料是依次排列的,每一行的像素三個通道的值依次排列,cv::Mat中的通道排列順序為BGR,那麼一個圖像需要的位址塊空間為uchar 寬×高×3.但是需要注意的是,有些處理器針對行數為4或8的圖像處理更有效率,是以為了提高效率就會填充一些額外的像素,這些額外的像素不被顯示和儲存,值是忽略的。

實作這個功能的代碼如下:

data[i]= data[i]/div*div+div/2; 通過整除的方式,就像素位數進行減少,這裡沒明白的是為啥後面還要加上div/2。

效果如下:

OpenCV&amp;Qt學習之三——圖像的初步處理

程式源代碼:

書中還給了其他十餘種操作的方法:

本文轉自emouse部落格園部落格,原文連結:http://www.cnblogs.com/emouse/archive/2013/03/31/2991333.html,如需轉載請自行聯系原作者

繼續閱讀