Ejemplo n.º 1
0
void insertImageCOI(InputArray _ch, CvArr* arr, int coi)
{
    Mat ch = _ch.getMat(), mat = cvarrToMat(arr, false, true, 1);
    if(coi < 0)
    {
        CV_Assert( CV_IS_IMAGE(arr) );
        coi = cvGetImageCOI((const IplImage*)arr)-1;
    }
    CV_Assert(ch.size == mat.size && ch.depth() == mat.depth() && 0 <= coi && coi < mat.channels());
    int _pairs[] = { 0, coi };
    mixChannels( &ch, 1, &mat, 1, _pairs, 1 );
}
Ejemplo n.º 2
0
void extractImageCOI(const CvArr* arr, OutputArray _ch, int coi)
{
    Mat mat = cvarrToMat(arr, false, true, 1);
    _ch.create(mat.dims, mat.size, mat.depth());
    Mat ch = _ch.getMat();
    if(coi < 0)
    {
        CV_Assert( CV_IS_IMAGE(arr) );
        coi = cvGetImageCOI((const IplImage*)arr)-1;
    }
    CV_Assert(0 <= coi && coi < mat.channels());
    int _pairs[] = { coi, 0 };
    mixChannels( &mat, 1, &ch, 1, _pairs, 1 );
}
Ejemplo n.º 3
0
/*
 * call-seq:
 *   set_coi(<i>coi</i>)
 *   set_coi(<i>coi</i>){|image| ...}
 *
 * Set COI. <i>coi</i> should be Fixnum.
 * Return self.
 */
VALUE
rb_set_coi(VALUE self, VALUE coi)
{
  VALUE block = rb_block_given_p() ? rb_block_proc() : 0;
  if (block) {
    int prev_coi = cvGetImageCOI(IPLIMAGE(self));
    cvSetImageCOI(IPLIMAGE(self), FIX2INT(coi));
    rb_yield_values(1, self);
    cvSetImageCOI(IPLIMAGE(self), prev_coi);
  } else {
    cvSetImageCOI(IPLIMAGE(self), FIX2INT(coi));
  }
  return self;
}
Ejemplo n.º 4
0
/* dst = src */
CV_IMPL void
cvCopy( const void* srcarr, void* dstarr, const void* maskarr )
{
    if( CV_IS_SPARSE_MAT(srcarr) && CV_IS_SPARSE_MAT(dstarr))
    {
        CV_Assert( maskarr == 0 );
        CvSparseMat* src1 = (CvSparseMat*)srcarr;
        CvSparseMat* dst1 = (CvSparseMat*)dstarr;
        CvSparseMatIterator iterator;
        CvSparseNode* node;

        dst1->dims = src1->dims;
        memcpy( dst1->size, src1->size, src1->dims*sizeof(src1->size[0]));
        dst1->valoffset = src1->valoffset;
        dst1->idxoffset = src1->idxoffset;
        cvClearSet( dst1->heap );

        if( src1->heap->active_count >= dst1->hashsize*CV_SPARSE_HASH_RATIO )
        {
            cvFree( &dst1->hashtable );
            dst1->hashsize = src1->hashsize;
            dst1->hashtable =
                (void**)cvAlloc( dst1->hashsize*sizeof(dst1->hashtable[0]));
        }

        memset( dst1->hashtable, 0, dst1->hashsize*sizeof(dst1->hashtable[0]));

        for( node = cvInitSparseMatIterator( src1, &iterator );
             node != 0; node = cvGetNextSparseNode( &iterator ))
        {
            CvSparseNode* node_copy = (CvSparseNode*)cvSetNew( dst1->heap );
            int tabidx = node->hashval & (dst1->hashsize - 1);
            CV_MEMCPY_AUTO( node_copy, node, dst1->heap->elem_size );
            node_copy->next = (CvSparseNode*)dst1->hashtable[tabidx];
            dst1->hashtable[tabidx] = node_copy;
        }
        return;
    }
    cv::Mat src = cv::cvarrToMat(srcarr, false, true, 1), dst = cv::cvarrToMat(dstarr, false, true, 1);
    CV_Assert( src.depth() == dst.depth() && src.size() == dst.size() );
    
    int coi1 = 0, coi2 = 0;
    if( CV_IS_IMAGE(srcarr) )
        coi1 = cvGetImageCOI((const IplImage*)srcarr);
    if( CV_IS_IMAGE(dstarr) )
        coi2 = cvGetImageCOI((const IplImage*)dstarr);
    
    if( coi1 || coi2 )
    {
        CV_Assert( (coi1 != 0 || src.channels() == 1) &&
            (coi2 != 0 || dst.channels() == 1) );
        
        int pair[] = { std::max(coi1-1, 0), std::max(coi2-1, 0) };
        cv::mixChannels( &src, 1, &dst, 1, pair, 1 );
        return;
    }
    else
        CV_Assert( src.channels() == dst.channels() );
    
    if( !maskarr )
        src.copyTo(dst);
    else
        src.copyTo(dst, cv::cvarrToMat(maskarr));
}
Ejemplo n.º 5
0
int cveGetImageCOI(IplImage* image)
{
   return cvGetImageCOI(image);
}
Ejemplo n.º 6
0
/*
 * Return COI as Fixnum.
 */
VALUE
rb_get_coi(VALUE self)
{
  return INT2FIX(cvGetImageCOI(IPLIMAGE(self)));
}
Ejemplo n.º 7
0
void BlobExtract::process( IplImage *img, std::vector<BlobExtract::Blob> *blobs )
{	bool new_blob;
	BlobExtract::Equivilence *e;
	int x, y;
	std::queue<BlobExtract::Equivilence*> to_visit;
	std::vector<BlobExtract::Equivilence> equivs;
	int l[5];
	int i, j;
	int blob_counter = 0;
	int channel;
	CvRect roi;
	int x_min, y_min, x_max, y_max;

	roi = cvGetImageROI( img );
	x_min = roi.x;
	y_min = roi.y;
	x_max = roi.x + roi.width;
	y_max = roi.y + roi.height;

	/* get the channel that is going to be processed, cvGetImageCOI returns 0
	   for all channels, if that is the case then just process the first channel */
	channel = cvGetImageCOI( img );
	if( channel != 0 )
		channel -= 1;

#if defined _DEBUG
	assert( channel < img->nChannels );
#endif

	this->create_map( img );
	this->clear_map();
	blobs->clear();

	for( y=y_min; y<y_max; ++y )
	{	for( x=x_min; x<x_max; ++x )
		{	l[4] = BE_PIXEL_I( img, x,   y,   channel ); // +------+------+------+
			l[3] = BE_PIXEL_I( img, x-1, y,   channel ); // | l[0] | l[1] | l[2] |
			l[1] = BE_PIXEL_I( img, x,   y-1, channel ); // +------+------+------+
			l[2] = BE_PIXEL_I( img, x+1, y-1, channel ); // | l[3] | l[4] |
			l[0] = BE_PIXEL_I( img, x-1, y-1, channel ); // +------+------+

			if( (uchar)img->imageData[l[4]] != 0 )
			{	new_blob = true;

				if( y>y_min )
				{	if( x>x_min && this->map[l[0]] != -1 )
					{	new_blob = false;

						this->map[l[4]] = this->map[l[0]];
					}

					if( this->map[l[1]] != -1 )
					{	new_blob = false;

						if( this->map[l[4]] == -1 )
							this->map[l[4]] = this->map[l[1]];

						if( this->map[l[4]] != this->map[l[1]] )
							this->add_equivilence( &equivs, this->map[l[4]], this->map[l[1]] );
					}

					if( x<x_max-1 && this->map[l[2]] != -1 )
					{	new_blob = false;

						if( this->map[l[4]] == -1 )
							this->map[l[4]] = this->map[l[2]];

						if( this->map[l[4]] != this->map[l[2]] )
							this->add_equivilence( &equivs, this->map[l[4]], this->map[l[2]] );
					}
				}

				if( x>x_min && this->map[l[3]] != -1 )
				{	new_blob = false;

					if( this->map[l[4]] == -1 )
						this->map[l[4]] = this->map[l[3]];

					if( this->map[l[4]] != this->map[l[3]] )
						this->add_equivilence( &equivs, this->map[l[4]], this->map[l[3]] );
				}

				if( new_blob )
				{	this->map[l[4]] = blob_counter;

					equivs.push_back( Equivilence() );
					equivs.back().id = blob_counter;

					blob_counter++;
				}
			}
		}
	}

	for( i=0; i<(int)equivs.size(); ++i )
		equivs[i].equiv = &(equivs[i]);

	// collapse the equiv values down
	for( i=0; i<(int)equivs.size(); ++i )
	{	for( j=0; j<(int)equivs.size(); ++j )
			equivs[j].visited = false;

		to_visit.push( &(equivs[i]) );

		while( !to_visit.empty() )
		{	e = to_visit.front();
			to_visit.pop();

			if( !e->visited )
			{	e->visited = true;

				if( e->id < equivs[i].equiv->id )
					equivs[i].equiv = e;

				for( j=0; j<(int)e->equals.size(); ++j )
				{	to_visit.push( &(equivs[e->equals[j]]) );
				}
			}
		}
	}

	// create the blobs
	for( i=0; i<(int)equivs.size(); ++i )
		if( &(equivs[i]) == equivs[i].equiv )
			blobs->push_back( BlobExtract::Blob( equivs[i].equiv->id ) );

	// store pointers to the correct blob in each equiv
	for( i=0; i<(int)blobs->size(); ++i )
	{	equivs[ (*blobs)[i].id ].blob = &((*blobs)[i]);
		(*blobs)[i].id = 0;
	}
			
	// get the information for each blob
	for( y=y_min; y<y_max; ++y )
		for( x=x_min; x<x_max; ++x )
		{	i = BE_PIXEL_I(img,x,y,channel);
			i = this->map[i];

			if( i != -1 )
				equivs[i].equiv->blob->addPixel( x, y );
		}
}