Class: OpenCV::IplImage

Inherits:
CvMat
  • Object
show all
Defined in:
ext/opencv/iplimage.cpp,
ext/opencv/iplimage.cpp

Overview

IPL(Intel Image Processing Library) Image class.

IplImage is subclass of CvMat. IplImage support ROI(region of interest) and COI(color of interest). Most of CvMat method support ROI, and some of CvMat method support COI.

What is ROI?

region of interest.

What is COI?

color of interest.

Constant Summary

Constants inherited from CvMat

CvMat::DRAWING_OPTION, CvMat::FIND_CONTOURS_OPTION, CvMat::FIND_FUNDAMENTAL_MAT_OPTION, CvMat::FLOOD_FILL_OPTION, CvMat::GOOD_FEATURES_TO_TRACK_OPTION, CvMat::OPTICAL_FLOW_BM_OPTION, CvMat::OPTICAL_FLOW_HS_OPTION

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CvMat

#[], #[]=, #abs_diff, #adaptive_threshold, #add, add_weighted, #and, #apply_color_map, #avg, #avg_sdv, #cam_shift, #canny, #channel, #circle, #circle!, #clone, compute_correspond_epilines, #convert_scale, #convert_scale_abs, #copy, #copy_make_border, #corner_eigenvv, #corner_harris, #corner_min_eigen_val, #count_non_zero, #create_mask, #cross_product, #data, #dct, #depth, #det, #dft, #diag, #dilate, #dilate!, #dim_size, #dims, #div, #dot_product, #draw_chessboard_corners, #draw_chessboard_corners!, #draw_contours, #draw_contours!, #each_col, #each_row, #eigenvv, #ellipse, #ellipse!, #ellipse_box, #ellipse_box!, #encode_image, #eq, #equalize_hist, #erode, #erode!, #extract_surf, #fill_convex_poly, #fill_convex_poly!, #fill_poly, #fill_poly!, #filter2d, #find_chessboard_corners, #find_contours, #find_contours!, #find_corner_sub_pix, find_fundamental_mat, find_homography, #flip, #flip!, #flood_fill, #flood_fill!, #ge, #get_cols, get_perspective_transform, #get_rows, #good_features_to_track, #gt, #height, #hough_circles, #hough_lines, #identity, #identity!, #in_range, #inpaint, #inside?, #integral, #invert, #laplace, #le, #line, #line!, #log_polar, #lt, #lut, #mat_mul, #match_shapes, #match_template, #mean_shift, merge, #method_missing, #min_max_loc, #moments, #morphology, #mul, #mul_transposed, #ne, norm, #normalize, #not, #not!, #optical_flow_bm, #optical_flow_hs, #optical_flow_lk, #or, #perspective_transform, #poly_line, #poly_line!, #pre_corner_detect, #put_text, #put_text!, #pyr_down, #pyr_mean_shift_filtering, #pyr_up, #quadrangle_sub_pix, #rand_shuffle, #rand_shuffle!, #range, #range!, #rect_sub_pix, #rectangle, #rectangle!, #remap, #repeat, #reshape, #resize, rotation_matrix2D, #save_image, #sdv, #set, #set!, #set_data, #set_zero, #set_zero!, #size, #smooth, #snake_image, #sobel, solve, #split, #square?, #sub, #sub_rect, #subspace_project, #subspace_reconstruct, #sum, #svd, #threshold, #to_16s, #to_16u, #to_32f, #to_32s, #to_64f, #to_8s, #to_8u, #to_CvMat, #to_IplConvKernel, #to_s, #trace, #transform, #transpose, #vector?, #warp_affine, #warp_perspective, #watershed, #width, #xor

Constructor Details

#new(width, height[, depth = CV_8U][, channel = 3]) ⇒ Object

Create width * height image. Each element-value set 0.

Each element possigle range is set by depth. Default is unsigned 8bit.

Number of channel is set by channel. channel should be 1..4.

note: width = col, height = row, on CvMat. It is noted not to make a mistake because the order of argument is differenct to CvMat.



55
56
57
58
59
60
61
62
63
64
# File 'ext/opencv/iplimage.cpp', line 55

VALUE
rb_initialize(int argc, VALUE *argv, VALUE self)
{
  VALUE width, height, depth, channel;
  rb_scan_args(argc, argv, "22", &width, &height, &depth, &channel);
  int _depth = CVMETHOD("DEPTH", depth, CV_8U);
  int _channel = argc < 4 ? 3 : NUM2INT(channel);
  DATA_PTR(self) = rb_cvCreateImage(cvSize(NUM2INT(width), NUM2INT(height)), cvIplDepth(_depth), _channel);
  return self;
}

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OpenCV::CvMat

Class Method Details

.decode_image(buf[, iscolor = CV_LOAD_IMAGE_COLOR]) ⇒ IplImage

Reads an image from a buffer in memory.

Parameters:

buf <CvMat, Array, String> - Input array
iscolor <Integer> - Flags specifying the color type of a decoded image (the same flags as CvMat#load)

Returns:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'ext/opencv/iplimage.cpp', line 115

VALUE
rb_decode_image(int argc, VALUE *argv, VALUE self)
{
  int iscolor, need_release;
  CvMat* buff = cCvMat::prepare_decoding(argc, argv, &iscolor, &need_release);
  IplImage* img_ptr = NULL;
  try {
    img_ptr = cvDecodeImage(buff, iscolor);
    if (need_release) {
      cvReleaseMat(&buff);
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return OPENCV_OBJECT(rb_klass, img_ptr);
}

.IplImage::load(filename[,iscolor = CV_LOAD_IMAGE_COLOR]) ⇒ Object

Load an image from file.

iscolor = CV_LOAD_IMAGE_COLOR, the loaded image is forced to be a 3-channel color image
iscolor = CV_LOAD_IMAGE_GRAYSCALE, the loaded image is forced to be grayscale
iscolor = CV_LOAD_IMAGE_UNCHANGED, the loaded image will be loaded as is.

Currently the following file format are supported.

  • Windows bitmaps - BMP,DIB

  • JPEG files - JPEG,JPG,JPE

  • Portable Network Graphics - PNG

  • Portable image format - PBM,PGM,PPM

  • Sun rasters - SR,RAS

  • TIFF files - TIFF,TIF



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/opencv/iplimage.cpp', line 82

VALUE
rb_load_image(int argc, VALUE *argv, VALUE self)
{
  VALUE filename, iscolor;
  rb_scan_args(argc, argv, "11", &filename, &iscolor);
  Check_Type(filename, T_STRING);

  int _iscolor;
  if (TYPE(iscolor) == T_NIL) {
    _iscolor = CV_LOAD_IMAGE_COLOR;
  }
  else {
    Check_Type(iscolor, T_FIXNUM);
    _iscolor = FIX2INT(iscolor);
  }
  
  IplImage *image;
  if ((image = cvLoadImage(StringValueCStr(filename), _iscolor)) == NULL) {
    rb_raise(rb_eStandardError, "file does not exist or invalid format image.");
  }
  return OPENCV_OBJECT(rb_klass, image);
}

Instance Method Details

#get_coiObject Also known as: coi

Return COI as Fixnum.



198
199
200
201
202
203
204
205
206
207
208
209
# File 'ext/opencv/iplimage.cpp', line 198

VALUE
rb_get_coi(VALUE self)
{
  int coi = 0;
  try {
    coi = cvGetImageCOI(IPLIMAGE(self));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return INT2FIX(coi);
}

#get_roiObject Also known as: roi

Get ROI as CvRect.



137
138
139
140
141
142
143
144
145
146
147
148
# File 'ext/opencv/iplimage.cpp', line 137

VALUE
rb_get_roi(VALUE self)
{
  CvRect rect;
  try {
    rect = cvGetImageROI(IPLIMAGE(self));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return cCvRect::new_object(rect);
}

#pyr_segmentation(level, threshold1, threshold2) ⇒ Array

Does image segmentation by pyramids. The pyramid builds up to the level <i>level<i>. The links between any pixel a on <i>level<i>i and its candidate father pixel b on the adjacent level are established if

p(c(a),c(b)) < threshold1. After the connected components are defined, they are joined into several clusters. Any two segments A and B belong to the same cluster, if
p(c(A),c(B)) < threshold2. The input image has only one channel, then
p(c^2,c^2)=|c^2-c^2|. If the input image has three channels (red, green and blue), then
p(c^2,c^2)=0,3*(c^2 r-c^2 r)+0.59*(c^2 g-c^2 g)+0,11*(c^2 b-c^2 b) . There may be more than one connected component per a cluster.

Return segmented image and sequence of connected components. support single-channel or 3-channel 8bit unsigned image only

Returns:

  • (Array)


576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'ext/opencv/iplimage.cpp', line 576

VALUE
rb_pyr_segmentation(VALUE self, VALUE level, VALUE threshold1, VALUE threshold2)
{
  IplImage* self_ptr = IPLIMAGE(self);
  CvSeq *comp = NULL;
  VALUE storage = cCvMemStorage::new_object();
  VALUE dest = Qnil;
  try {
    dest = cIplImage::new_object(cvGetSize(self_ptr), cvGetElemType(self_ptr));
    cvPyrSegmentation(self_ptr, IPLIMAGE(dest), CVMEMSTORAGE(storage), &comp,
		      NUM2INT(level), NUM2DBL(threshold1), NUM2DBL(threshold2));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  if (!comp) {
    comp = cvCreateSeq(CV_SEQ_CONNECTED_COMP, sizeof(CvSeq), sizeof(CvConnectedComp), CVMEMSTORAGE(storage));
  }
  return rb_ary_new3(2, dest, cCvSeq::new_sequence(cCvSeq::rb_class(), comp, cCvConnectedComp::rb_class(), storage));
}

#reset_coiObject

Reset COI setting. Same as IplImage#coi = 0. Return self.



243
244
245
246
247
248
249
250
251
252
253
# File 'ext/opencv/iplimage.cpp', line 243

VALUE
rb_reset_coi(VALUE self)
{
  try {
    cvSetImageCOI(IPLIMAGE(self), 0);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}

#reset_roiObject

Reset ROI setting. Same as IplImage#roi = nil. Return self.



183
184
185
186
187
188
189
190
191
192
193
# File 'ext/opencv/iplimage.cpp', line 183

VALUE
rb_reset_roi(VALUE self)
{
  try {
    cvResetImageROI(IPLIMAGE(self));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}

#set_coi(coi) ⇒ Object #set_coi(coi) {|image| ... } ⇒ Object Also known as: coi=

Set COI. coi should be Fixnum. Return self.

Overloads:

  • #set_coi(coi) {|image| ... } ⇒ Object

    Yields:

    • (image)


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'ext/opencv/iplimage.cpp', line 219

VALUE
rb_set_coi(VALUE self, VALUE coi)
{
  VALUE block = rb_block_given_p() ? rb_block_proc() : 0;
  try {
    if (block) {
      int prev_coi = cvGetImageCOI(IPLIMAGE(self));
      cvSetImageCOI(IPLIMAGE(self), NUM2INT(coi));
      rb_yield_values(1, self);
      cvSetImageCOI(IPLIMAGE(self), prev_coi);
    }
    else {
      cvSetImageCOI(IPLIMAGE(self), NUM2INT(coi));
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}

#set_roi(rect) ⇒ Object #set_roi(rect) {|image| ... } ⇒ Object Also known as: roi=

Set ROI. rect should be CvRect or compatible object. Return self.

Overloads:

  • #set_roi(rect) {|image| ... } ⇒ Object

    Yields:

    • (image)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'ext/opencv/iplimage.cpp', line 158

VALUE
rb_set_roi(VALUE self, VALUE roi)
{
  VALUE block = rb_block_given_p() ? rb_block_proc() : 0;
  try {
    if (block) {
      CvRect prev_roi = cvGetImageROI(IPLIMAGE(self));
      cvSetImageROI(IPLIMAGE(self), VALUE_TO_CVRECT(roi));
      rb_yield_values(1, self);
      cvSetImageROI(IPLIMAGE(self), prev_roi);
    }
    else {
      cvSetImageROI(IPLIMAGE(self), VALUE_TO_CVRECT(roi));
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}

#smoothness(lowFreqRatio, blankDensity, messyDensity, highFreqRatio) ⇒ Array, Float

Determines if the image’s smoothness is either, :smooth, :messy, or :blank.

Original Author: [email protected]

Returns:

  • (Array, Float)


263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'ext/opencv/iplimage.cpp', line 263

VALUE
rb_smoothness(int argc, VALUE *argv, VALUE self)
{
  VALUE lowFreqRatio, blankDensity, messyDensity, highFreqRatio;
  rb_scan_args(argc, argv, "04", &lowFreqRatio, &blankDensity, &messyDensity, &highFreqRatio);

  double f_lowFreqRatio, f_blankDensity, f_messyDensity, f_highFreqRatio;
  double outLowDensity, outHighDensity;
  if (TYPE(lowFreqRatio) == T_NIL) {
    f_lowFreqRatio = 10 / 128.0f;
  }
  else {
    Check_Type(lowFreqRatio, T_FLOAT);
    f_lowFreqRatio = NUM2DBL(lowFreqRatio);
  }
  if (TYPE(blankDensity) == T_NIL) {
    f_blankDensity = 1.2f;
  }
  else {
    Check_Type(blankDensity, T_FLOAT);
    f_blankDensity = NUM2DBL(blankDensity);
  }
  if (TYPE(messyDensity) == T_NIL) {
    f_messyDensity = 0.151f;
  }
  else {
    Check_Type(messyDensity, T_FLOAT);
    f_messyDensity = NUM2DBL(messyDensity);
  }
  if (TYPE(highFreqRatio) == T_NIL) {
    f_highFreqRatio = 5 / 128.0f;
  }
  else {
    Check_Type(highFreqRatio, T_FLOAT);
    f_highFreqRatio = NUM2DBL(highFreqRatio);
  }

  IplImage *pFourierImage;
  IplImage *p64DepthImage;

  // the image is required to be in depth of 64
  if (IPLIMAGE(self)->depth == 64) {
    p64DepthImage = NULL;
    pFourierImage = create_fourier_image(IPLIMAGE(self));
  }
  else {
    p64DepthImage = rb_cvCreateImage(cvGetSize(IPLIMAGE(self)), IPL_DEPTH_64F, 1);
    cvConvertScale(CVARR(self), p64DepthImage, 1.0, 0.0);
    pFourierImage = create_fourier_image(p64DepthImage);
  }

  Smoothness result = compute_smoothness(pFourierImage, f_lowFreqRatio, f_blankDensity, f_messyDensity,
					 f_highFreqRatio, outLowDensity, outHighDensity);

  cvReleaseImage(&pFourierImage);
  if (p64DepthImage != NULL)
    cvReleaseImage(&p64DepthImage);

  switch(result) {
  case SMOOTH:
    return rb_ary_new3(3, ID2SYM(rb_intern("smooth")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
  case MESSY:
    return rb_ary_new3(3, ID2SYM(rb_intern("messy")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
  case BLANK:
    return rb_ary_new3(3, ID2SYM(rb_intern("blank")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
  default:
    return rb_ary_new3(3, NULL, rb_float_new(outLowDensity), rb_float_new(outHighDensity));
  }
}