prepress-tips
GTK+ で openCV
最終更新:
prepress-tips
-
view
最終更新日時: 2010/10/17
このページは 作成中です。
開発環境
- 開発環境は Ubuntu 10.04 LTS ( ubuntu LiveUSB で利用 )。
- openCV は簡単にインストールできる。
- その他に必要なものも ほぼ最初から入っている。
( なくても簡単に追加インストールできる。 )
export DISPLAY=:0.0
export DISPLAY=localhost:10.0
devhelp &
firefox http://opencv.jp/opencv-2svn/c/ &
firefox http://opencv.jp/opencv-2svn/c/ &
gcc -o [実行ファイル] [ソースファイル] `pkg-config gtk+-2.0 opencv --cflags --libs`
教科書
- 次の2冊。どちらも 初心者にはとてもわかりやすい。
- 入門GTK+
- Essential OpenCV Programming with Visual C++ 2008
GTK+ のライセンス
- GTK+ のライセンスは GNU LGPL 2.1 。
- このページのプログラムも このライセンスに準じる。
openCV のライセンス
- openCV のライセンスは 以下のとおり。
- このページのプログラムも このライセンスに準じる。
IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
By downloading, copying, installing or using the software you agree to this license.
If you do not agree to this license, do not download, install,
copy or use the software.
License Agreement
For Open Source Computer Vision Library
Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
Copyright (C) 2008-2010, Willow Garage Inc., all rights reserved.
Third party copyrights are property of their respective owners.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistribution's of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistribution's in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name of the copyright holders may not be used to endorse or promote products
derived from this software without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
In no event shall the Intel Corporation or contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
By downloading, copying, installing or using the software you agree to this license.
If you do not agree to this license, do not download, install,
copy or use the software.
License Agreement
For Open Source Computer Vision Library
Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
Copyright (C) 2008-2010, Willow Garage Inc., all rights reserved.
Third party copyrights are property of their respective owners.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistribution's of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistribution's in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name of the copyright holders may not be used to endorse or promote products
derived from this software without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
In no event shall the Intel Corporation or contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
ex01
- 画像を読み込み表示するプログラム。モノクロで表示する機能付き。
- 先ほどの書籍のサンプルを ubuntu で実行できるよう修正したもの。
* 変数の定義 / ? WIN ? img_src ? Form1 * 関数の定義 / ? btnLoad_Click // ? btnGray_Click // ? btnReset_Click // ? Form1_Load // ? Form1_FormClosed // ? Form1 - quit // / Form1_FormClosed ; * 処理 / / Form1 - new ? Form1 - vbox // ? Form1 - hbox1 // ? Form1 - btnLoad // ? Form1 - hbox2 // ? Form1 - btnGray // ? Form1 - btnReset // / Form1_Load ; / Form1 - run /name ex01.cpp ubuntu版 / ex01.c // このプログラムは 以下の書籍のサンプルを ubuntu で実行できるよう修正したものです。 // Essential OpenCV Programming with Visual C++ 2008 // http://www.cutt.co.jp/book/978-4-87783-232-2.html #include <cv.h> #include <cxcore.h> #include <highgui.h> #include <gtk/gtk.h> / 変数の定義 / 関数の定義 int main ( int argc , char* argv[] ) { gtk_init ( &argc , &argv ) ; / 処理 return 0 ; } /name 未指定 / /use ex01_detail.txt
/?- btn_Click , WIN , img_src , img_gray * btnLoad_Click // void btnLoad_Click ( ) ? openFileDialog1 - new // / openFileDialog1 - show ? openFileDialog1 - result // / if( ! openFileDialog1 - result OK ) { openFileDialog1 - free ; return ; } ? fname - from openFileDialog1 // / openFileDialog1 - free / img_src - load fname / img_src - show / fname -free * btnGray_Click // void btnGray_Click ( ) ? img_gray - new // / img_gray - from img_src / img_gray - show / img_gray - free * btnReset_Click // void btnReset_Click ( ) / img_src - show * Form1_Load // void Form1_Load ( ) / WIN - new * Form1_FormClosed // void Form1_FormClosed ( ) / WIN - close / img_src - free * WIN / gchar* WIN ="ex01" ; * WIN - new / cvNamedWindow( WIN , CV_WINDOW_AUTOSIZE ) ; * WIN - close / cvDestroyWindow( WIN ) ; * img_src / IplImage* img_src ; * img_src - free / cvReleaseImage( &img_src ) ; * img_src - load fname / img_src = cvLoadImage( fname , CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR ) ; * img_src - show / cvShowImage( WIN , img_src ) ; * img_gray - new // IplImage* img_gray ; * img_gray - from img_src / img_gray = cvCreateImage( cvGetSize( img_src ) , IPL_DEPTH_8U , 1 ) ; cvCvtColor( img_src , img_gray , CV_BGR2GRAY ) ; * img_gray - free / cvReleaseImage( &img_gray ) ; * img_gray - show / cvShowImage( WIN , img_gray ) ;
/?- openFileDialog1 , fname , Form1 * openFileDialog1 - new // GtkWidget* openFileDialog1 ; openFileDialog1 = gtk_file_chooser_dialog_new( "load image" , GTK_WINDOW( Form1 ) , GTK_FILE_CHOOSER_ACTION_OPEN , GTK_STOCK_CANCEL , GTK_RESPONSE_CANCEL , GTK_STOCK_OPEN , GTK_RESPONSE_ACCEPT , NULL ); * openFileDialog1 - show / gtk_widget_show_all( openFileDialog1 ) ; * openFileDialog1 - result // gint openFileDialog1_result ; openFileDialog1_result = gtk_dialog_run( GTK_DIALOG( openFileDialog1 ) ) ; * openFileDialog1 - result OK / ( openFileDialog1_result == GTK_RESPONSE_ACCEPT ) * openFileDialog1 - free / gtk_widget_destroy( openFileDialog1 ) ; * fname - from openFileDialog1 // gchar* fname ; fname = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( openFileDialog1 ) ) ; * fname -free / g_free( fname ) ; * Form1 / GtkWidget* Form1 ; * Form1 - new / Form1 = gtk_window_new( GTK_WINDOW_TOPLEVEL ) ; gtk_window_set_title ( GTK_WINDOW( Form1 ) , "Form1" ) ; gtk_widget_set_size_request( Form1 , 160 , 80 ) ; g_signal_connect( G_OBJECT( Form1 ) , "destroy" , G_CALLBACK( Form1_quit ) , NULL ) ; * Form1 - run / gtk_widget_show_all( Form1 ) ; gtk_main() ; * Form1 - quit // void Form1_quit ( ) gtk_main_quit () ; * Form1 - vbox // GtkWidget* vbox ; vbox = gtk_vbox_new( FALSE , 3 ) ; gtk_container_add( GTK_CONTAINER( Form1 ) , vbox ) ; gtk_container_set_border_width( GTK_CONTAINER( vbox ) , 6 ) ; * Form1 - hbox1 // GtkWidget* hbox1 ; hbox1 = gtk_hbox_new( FALSE , 5 ) ; gtk_box_pack_start( GTK_BOX( vbox ) , hbox1 , TRUE , FALSE , 2 ) ; * Form1 - hbox2 // GtkWidget* hbox2 ; hbox2 = gtk_hbox_new( FALSE , 0 ) ; gtk_box_pack_start( GTK_BOX( vbox ) , hbox2 , TRUE , FALSE , 2 ) ; * Form1 - btnLoad // GtkWidget* btnLoad ; btnLoad = gtk_button_new_with_label( "画像読込" ) ; gtk_box_pack_start( GTK_BOX( hbox1 ) , btnLoad , FALSE , FALSE , 6 ) ; g_signal_connect( G_OBJECT( btnLoad ) , "clicked" , G_CALLBACK( btnLoad_Click ) , NULL ) ; * Form1 - btnGray // GtkWidget* btnGray ; btnGray = gtk_button_new_with_label( " モノクロ " ) ; gtk_box_pack_start( GTK_BOX( hbox2 ) , btnGray , FALSE , FALSE , 6 ) ; g_signal_connect( G_OBJECT( btnGray ) , "clicked" , G_CALLBACK( btnGray_Click ) , NULL ) ; * Form1 - btnReset // GtkWidget* btnReset ; btnReset = gtk_button_new_with_label( "元に戻す" ) ; gtk_box_pack_start( GTK_BOX( hbox2 ) , btnReset , FALSE , FALSE , 6 ) ; g_signal_connect( G_OBJECT( btnReset ) , "clicked" , G_CALLBACK( btnReset_Click ) , NULL ) ;
ex01_gtk
- 画像を読み込み モノクロ変換し 保存するプログラム。
- 先ほどのプログラムに 保存機能を加え 全面的に書き直したもの。
ソースを少し修正すれば チャネルミキシングなども試せる。
* 変数の定義 / ? 画像ウィンドウ ? 操作パネル ? 元画像 ? 変換画像 * 関数の定義 / ? 画像読込ボタン - onClick ? 選択ダイアログ - new load // / 選択ダイアログ - show ? 選択ダイアログ - result // / if( 選択ダイアログ - cancel ) { 選択ダイアログ - free ; return ; } / 選択ダイアログ - set 元画像 fname ; 選択ダイアログ - free / 元画像 - free / 元画像 - new load / 画像ウィンドウ - 画像 free / 画像ウィンドウ - new 元画像 ; 画像ウィンドウ - show ? モノクロボタン - onClick / 変換画像 - free / 変換画像 - new ; 変換画像 - conv / 画像ウィンドウ - 画像 free / 画像ウィンドウ - new 変換画像 ; 画像ウィンドウ - show ? 元に戻すボタン - onClick / 画像ウィンドウ - 画像 free / 画像ウィンドウ - new 元画像 ; 画像ウィンドウ - show ? 画像保存ボタン - onClick / if( 変換画像 - 未変換 ) { return ; } ? 選択ダイアログ - new save // / 選択ダイアログ - show ? 選択ダイアログ - result // / if( 選択ダイアログ - cancel ) { 選択ダイアログ - free ; return ; } / 選択ダイアログ - set 変換画像 fname ; 選択ダイアログ - free / 変換画像 - save ? 操作パネル - quit // / 元画像 - free ; 変換画像 - free * 処理 / / 操作パネル - new ? 操作パネル - vbox // ? 操作パネル - hbox1 // ? 操作パネル - 画像読込ボタン // ? 操作パネル - 画像保存ボタン // ? 操作パネル - hbox2 // ? 操作パネル - モノクロボタン // ? 操作パネル - 元に戻すボタン // / 画像ウィンドウ - new / 画像ウィンドウ - new canvas ; 画像ウィンドウ - show / 操作パネル - run /name ex01.c gtk版 / ex01_gtk.c #include <cv.h> #include <cxcore.h> #include <highgui.h> #include <gtk/gtk.h> / 変数の定義 / 関数の定義 int main ( int argc , char* argv[] ) { gtk_init ( &argc , &argv ) ; / 処理 return 0 ; } /name 未指定 / /use ex01_gtk_detail.txt
/?- 元画像 * 元画像 / gchar* img_src_fname ; GdkPixbuf* img_src ; IplImage* img_src_cv ; * 元画像 - fname / img_src_fname * 元画像 - new load / img_src = gdk_pixbuf_new_from_file( img_src_fname , NULL ) ; ? 元画像 - w , h , ch , rs // img_src_cv = cvCreateImageHeader( cvSize( img_w, img_h ) , IPL_DEPTH_8U, img_ch ); img_src_cv -> imageData = gdk_pixbuf_get_pixels( img_src ) ; g_free( img_src_fname ) ; * 元画像 - free / if( img_src != NULL ) { cvReleaseImageHeader( &img_src_cv ) ; g_object_unref( G_OBJECT( img_src ) ) ; } * 元画像 - w , h , ch , rs // *~ gint img_w ; gint img_h ; gint img_ch ; gint img_rs ; img_w = gdk_pixbuf_get_width( img_src ) ; img_h = gdk_pixbuf_get_height( img_src ) ; img_ch = gdk_pixbuf_get_n_channels( img_src ) ; img_rs = gdk_pixbuf_get_rowstride( img_src ) ; /?- 変換画像 * 変換画像 / gchar* img_conv_fname ; GdkPixbuf* img_conv ; IplImage* img_conv_cv ; * 変換画像 - fname / img_conv_fname * 変換画像 - 未変換 / ( img_conv == NULL ) * 変換画像 - new / ? 元画像 - w , h , ch , rs // img_conv_cv = cvCreateImage( cvSize( img_w, img_h ) , IPL_DEPTH_8U, 3 ); img_conv = gdk_pixbuf_new_from_data( img_conv_cv -> imageData , GDK_COLORSPACE_RGB , FALSE , 8 , img_w , img_h , img_conv_cv -> widthStep , NULL , NULL ); * 変換画像 - free / if( img_conv != NULL ) { cvReleaseImage( &img_conv_cv ) ; g_object_unref( G_OBJECT( img_conv ) ) ; } * 変換画像 - conv / / いろいろな変換 - rgb to gray * いろいろな変換 - rgb to bgr / cvCvtColor( img_src_cv , img_conv_cv , CV_RGB2BGR ) ; * いろいろな変換 - rgb to gray / IplImage* img_gray_cv ; img_gray_cv = cvCreateImage( cvSize( img_w, img_h ) , IPL_DEPTH_8U, 1 ) ; cvCvtColor( img_src_cv , img_gray_cv , CV_RGB2GRAY ) ; cvCvtColor( img_gray_cv , img_conv_cv , CV_GRAY2RGB ) ; cvReleaseImage( &img_gray_cv ) ; * いろいろな変換 - rgb to gray 2 / CvMat* rgb_2_bgr ; rgb_2_bgr = cvCreateMat( img_ch , img_ch , CV_32FC1 ) ; cvSetZero( rgb_2_bgr ) ; cvmSet( rgb_2_bgr , 0 , 2 , 1.0 ) ; cvmSet( rgb_2_bgr , 1 , 1 , 1.0 ) ; cvmSet( rgb_2_bgr , 2 , 0 , 1.0 ) ; CvMat* offset ; offset = cvCreateMat( img_ch , 1 , CV_32FC1 ) ; cvSet( offset , cvScalarAll( 0.0 ) , NULL ) ; // cvSetZero( offset ) IplImage* img_bgr_cv ; IplImage* img_gray_cv ; img_bgr_cv = cvCreateImage( cvSize( img_w, img_h ) , IPL_DEPTH_8U, img_ch ) ; img_gray_cv = cvCreateImage( cvSize( img_w, img_h ) , IPL_DEPTH_8U, 1 ) ; cvTransform( img_src_cv , img_bgr_cv , rgb_2_bgr , offset ) ; cvCvtColor( img_bgr_cv , img_gray_cv , CV_BGR2GRAY ) ; cvReleaseImage( &img_bgr_cv ) ; cvReleaseImage( &img_gray_cv ) ; cvReleaseMat( &rgb_2_bgr ) ; cvReleaseMat( &offset ) ; * 変換画像 - save / gdk_pixbuf_save( img_conv , img_conv_fname , "tiff" , NULL , "compression" , "5" , NULL // LZW ) ; g_free( img_conv_fname ) ;
/?- 画像ウィンドウ * 画像ウィンドウ / GtkWidget* WIN_img ; GtkWidget* WIN_canvas ; GtkWidget* WIN ; * 画像ウィンドウ - new / WIN = gtk_window_new( GTK_WINDOW_TOPLEVEL ) ; gtk_window_set_title( GTK_WINDOW( WIN ) , "画像" ) ; gtk_window_set_default_size( GTK_WINDOW( WIN ) , 400 , 300 ) ; gtk_window_move( GTK_WINDOW( WIN ) , 0 , 140 ) ; * 画像ウィンドウ - show / gtk_widget_show_all( WIN ) ; * 画像ウィンドウ - new canvas / WIN_canvas = gtk_scrolled_window_new( NULL , NULL ) ; gtk_scrolled_window_set_placement( GTK_SCROLLED_WINDOW ( WIN_canvas ) , GTK_CORNER_TOP_LEFT ) ; gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW ( WIN_canvas ) , GTK_POLICY_AUTOMATIC , GTK_POLICY_AUTOMATIC ) ; gtk_container_add ( GTK_CONTAINER( WIN ) , WIN_canvas ) ; * 画像ウィンドウ - new 元画像 / WIN_img = gtk_image_new_from_pixbuf( img_src ) ; gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW ( WIN_canvas ) , WIN_img ) ; * 画像ウィンドウ - new 変換画像 / WIN_img = gtk_image_new_from_pixbuf( img_conv ) ; gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW ( WIN_canvas ) , WIN_img ) ; * 画像ウィンドウ - 画像 free / if( WIN_img != NULL ) { gtk_object_destroy( GTK_OBJECT( WIN_img ) ) ; } /?- 操作パネル * 操作パネル / GtkWidget* Panel ; * 操作パネル - new / Panel = gtk_window_new( GTK_WINDOW_TOPLEVEL ) ; gtk_window_set_title ( GTK_WINDOW( Panel ) , "操作パネル" ) ; gtk_widget_set_size_request( Panel , 170 , 80 ) ; g_signal_connect( G_OBJECT( Panel ) , "destroy" , G_CALLBACK( Panel_quit ) , NULL ) ; * 操作パネル - run / gtk_widget_show_all( Panel ) ; gtk_main() ; * 操作パネル - quit // void Panel_quit ( ) gtk_main_quit () ; * 操作パネル - vbox // GtkWidget* vbox ; vbox = gtk_vbox_new( FALSE , 3 ) ; gtk_container_add( GTK_CONTAINER( Panel ) , vbox ) ; gtk_container_set_border_width( GTK_CONTAINER( vbox ) , 6 ) ; * 操作パネル - hbox1 // GtkWidget* hbox1 ; hbox1 = gtk_hbox_new( FALSE , 0 ) ; gtk_box_pack_start( GTK_BOX( vbox ) , hbox1 , TRUE , FALSE , 2 ) ; * 操作パネル - hbox2 // GtkWidget* hbox2 ; hbox2 = gtk_hbox_new( FALSE , 0 ) ; gtk_box_pack_start( GTK_BOX( vbox ) , hbox2 , TRUE , FALSE , 2 ) ; * 操作パネル - 画像読込ボタン // GtkWidget* buttonLoad ; buttonLoad = gtk_button_new_with_label( "画像読込" ) ; gtk_box_pack_start( GTK_BOX( hbox1 ) , buttonLoad , FALSE , FALSE , 6 ) ; g_signal_connect( G_OBJECT( buttonLoad ) , "clicked" , / G_CALLBACK( & 画像読込ボタン - onClick ) , NULL ) ; * 操作パネル - 画像保存ボタン // GtkWidget* buttonSave ; buttonSave = gtk_button_new_with_label( "画像保存" ) ; gtk_box_pack_start( GTK_BOX( hbox1 ) , buttonSave , FALSE , FALSE , 6 ) ; g_signal_connect( G_OBJECT( buttonSave ) , "clicked" , / G_CALLBACK( & 画像保存ボタン - onClick ) , NULL ) ; * 操作パネル - モノクロボタン // GtkWidget* buttonGray ; buttonGray = gtk_button_new_with_label( " モノクロ " ) ; gtk_box_pack_start( GTK_BOX( hbox2 ) , buttonGray , FALSE , FALSE , 6 ) ; g_signal_connect( G_OBJECT( buttonGray ) , "clicked" , / G_CALLBACK( & モノクロボタン - onClick ) , NULL ) ; * 操作パネル - 元に戻すボタン // GtkWidget* buttonReset ; buttonReset = gtk_button_new_with_label( "元に戻す" ) ; gtk_box_pack_start( GTK_BOX( hbox2 ) , buttonReset , FALSE , FALSE , 6 ) ; g_signal_connect( G_OBJECT( buttonReset ) , "clicked" , / G_CALLBACK( & 元に戻すボタン - onClick ) , NULL ) ; * 画像読込ボタン - onClick // void buttonLoad_onClick ( ) * & 画像読込ボタン - onClick / buttonLoad_onClick * モノクロボタン - onClick // void buttonGray_onClick ( ) * & モノクロボタン - onClick / buttonGray_onClick * 元に戻すボタン - onClick // void buttonReset_onClick ( ) * & 元に戻すボタン - onClick / buttonReset_onClick * 画像保存ボタン - onClick // void buttonSave_onClick ( ) * & 画像保存ボタン - onClick / buttonSave_onClick /?- 選択ダイアログ * 選択ダイアログ - new load // GtkWidget* file_dialog ; file_dialog = gtk_file_chooser_dialog_new( "画像選択" , GTK_WINDOW( Panel ) , GTK_FILE_CHOOSER_ACTION_OPEN , GTK_STOCK_CANCEL , GTK_RESPONSE_CANCEL , GTK_STOCK_OPEN , GTK_RESPONSE_ACCEPT , NULL ); * 選択ダイアログ - new save // GtkWidget* file_dialog ; file_dialog = gtk_file_chooser_dialog_new( "画像選択" , GTK_WINDOW( Panel ) , GTK_FILE_CHOOSER_ACTION_SAVE , GTK_STOCK_CANCEL , GTK_RESPONSE_CANCEL , GTK_STOCK_SAVE , GTK_RESPONSE_ACCEPT , NULL ); gtk_file_chooser_set_current_name( GTK_FILE_CHOOSER( file_dialog ) , "untitled.tif" ) ; gtk_file_chooser_set_do_overwrite_confirmation( GTK_FILE_CHOOSER( file_dialog ) , TRUE ) ; * 選択ダイアログ - show / gtk_widget_show_all( file_dialog ) ; * 選択ダイアログ - free / gtk_widget_destroy( file_dialog ) ; * 選択ダイアログ - result // gint file_dialog_result ; file_dialog_result = gtk_dialog_run( GTK_DIALOG( file_dialog ) ) ; * 選択ダイアログ - cancel / ( file_dialog_result != GTK_RESPONSE_ACCEPT ) * 選択ダイアログ - set 元画像 fname / / 元画像 - fname = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( file_dialog ) ) ; * 選択ダイアログ - set 変換画像 fname / / 変換画像 - fname = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( file_dialog ) ) ;
openCV に日本語文字を描画
- openCV に GTK+ で日本語文字を描画する。
#include <cv.h> #include <cxcore.h> #include <highgui.h> #include <gtk/gtk.h> // gcc -o draw_text draw_text.c `pkg-config gtk+-2.0 opencv --cflags --libs` // ./draw_text int main ( int argc , char* argv[] ) { gtk_init ( &argc , &argv ) ; gint img_w ; gint img_h ; // サイズ img_w = 260 ; img_h = 60 ; IplImage* img_org_cv ; // 元の画像 - openCV で作成 img_org_cv = cvCreateImage( cvSize( img_w, img_h ) , IPL_DEPTH_8U, 3 ); cvSet( img_org_cv , cvScalarAll( 255 ) , NULL ) ; GdkPixbuf* img_org ; // 元の画像 - pixbuf に変換 img_org = gdk_pixbuf_new_from_data( img_org_cv -> imageData , GDK_COLORSPACE_RGB , FALSE , 8 , img_w , img_h , img_org_cv -> widthStep , NULL , NULL ); GdkPixmap* pixmap ; // 元の画像 - pixmap に変換 gdk_pixbuf_render_pixmap_and_mask( img_org , &pixmap , NULL , 255 ) ; GdkColormap* colormap ; GdkGC* gc ; colormap = gdk_drawable_get_colormap( GDK_DRAWABLE( pixmap ) ) ; gc = gdk_gc_new( GDK_DRAWABLE( pixmap ) ) ; PangoFontDescription* font_desc ; // 描画情報 - フォントを指定 font_desc = pango_font_description_from_string( "Arial Bold 14" ) ; gchar* markup_str ; // 描画情報 - 内容を指定 markup_str = g_strdup_printf( "%s%s%s\x00" , "<span font='14'>日本語文字を</span>" , "<span background = 'red' foreground= 'yellow' font='20'>描画</span>" , "<span font='14'>してみる</span>" , "" ) ; PangoLayout* pango_layout ; // 描画情報 - pixmap に描画 GtkWidget* area ; area = gtk_drawing_area_new() ; pango_layout = gtk_widget_create_pango_layout( GTK_WIDGET( area ) , "" ) ; pango_layout_set_markup( pango_layout , markup_str , -1 ) ; gdk_draw_layout( GDK_DRAWABLE( pixmap ) , gc , 10 , 10 , pango_layout ) ; gtk_object_destroy( GTK_OBJECT( area ) ) ; g_object_unref( G_OBJECT( colormap ) ) ; g_object_unref( G_OBJECT( gc ) ) ; g_free( markup_str ) ; GdkPixbuf* img_text ; // 描画情報 - pixbuf に変換 img_text = gdk_pixbuf_get_from_drawable( NULL , GDK_DRAWABLE( pixmap ) , NULL , 0 , 0 , 0 , 0 , img_w , img_h ) ; IplImage* img_text_cv ; // 描画情報 - openCV に変換 img_text_cv = cvCreateImageHeader( cvSize( img_w , img_h ) , IPL_DEPTH_8U, 3 ); img_text_cv -> imageData = gdk_pixbuf_get_pixels( img_text ) ; cvCvtColor( img_text_cv , img_text_cv , CV_RGB2BGR ) ; // 画面に表示 - openCV で cvNamedWindow( "openCVに GTK+で描画" , CV_WINDOW_AUTOSIZE ) ; cvShowImage( "openCVに GTK+で描画" , img_text_cv ) ; cvWaitKey( 0 ) ; cvDestroyWindow( "openCVに GTK+で描画" ) ; // 終了 cvReleaseImageHeader( &img_text_cv ) ; g_object_unref( G_OBJECT( img_text ) ) ; cvReleaseImage( &img_org_cv ) ; g_object_unref( G_OBJECT( img_org ) ) ; return 0 ; }