Exemplo n.º 1
0
Array<T> createSubArray(const Array<T> &parent, const vector<af_seq> &index,
                        bool copy) {
    parent.eval();

    dim4 dDims          = parent.getDataDims();
    dim4 dStrides       = calcStrides(dDims);
    dim4 parent_strides = parent.strides();

    if (dStrides != parent_strides) {
        const Array<T> parentCopy = copyArray(parent);
        return createSubArray(parentCopy, index, copy);
    }

    dim4 pDims   = parent.dims();
    dim4 dims    = toDims(index, pDims);
    dim4 strides = toStride(index, dDims);

    // Find total offsets after indexing
    dim4 offsets = toOffset(index, pDims);
    dim_t offset = parent.getOffset();
    for (int i = 0; i < 4; i++) offset += offsets[i] * parent_strides[i];

    Array<T> out = Array<T>(parent, dims, offset, strides);

    if (!copy) return out;

    if (strides[0] != 1 || strides[1] < 0 || strides[2] < 0 || strides[3] < 0) {
        out = copyArray(out);
    }

    return out;
}
Exemplo n.º 2
0
void prepareArrayWithIndex(dynamicArray *anArray, int anIndex)
{
	while (anIndex >= 0)
	{
		//calculate a size
		int theSize = 10; //<!-- by default
		
		if (NULL != anArray->lastSub)
		{
			theSize = anArray->lastSub->subSize * 2;
		}
	
		subArray *theSlot = createSubArray(theSize);
		addSub(anArray, theSlot);
	
		anIndex -= theSize;
	}
}
Exemplo n.º 3
0
char operateImage(Userdata *userdata) {
	if (!userdata) {
		return 0;
	}

	IplImage *image1 = userdata->input[0];
	IplImage *image2 = userdata->input[1];
	IplImage *imageOut = userdata->output[0];
	IplImage *imageOut2 = userdata->output[1];

	static int color_mode = 4;
	static int smooth_mode = 0;
	static int otsu_mode = 0;
	static int close_mode = 0;
	static int canny_mode = 0;
	static int contour_mode = 0;
	static int hsv_mode = 0;
	static int save_img = 0;
	static int history_mode = 0;

	int key = userdata->key;
	switch (key) {
	case 'g':
		color_mode++;
		color_mode %= 5;
		break;
	case 's':
		smooth_mode = !smooth_mode;
		break;
	case 'o':
		otsu_mode = !otsu_mode;
		break;
	case 'e':
		close_mode = !close_mode;
		break;
	case 'c':
		canny_mode = !canny_mode;
		break;
	case 'b':
		contour_mode = !contour_mode;
		break;
	case 'h':
		hsv_mode = !hsv_mode;
		break;
	case 'H':
		history_mode = !history_mode;
		break;
	case 'S':
		save_img = 1;
		break;
	default:
		//cout << key << "\n";
		break;
	}

	int value = userdata->value;
	int kernelSize = userdata->kernelSize;
	kernelSize += 1 - (kernelSize % 2);
	int lowThreshold = userdata->lowThreshold;
	int highThreshold = userdata->highThreshold;
	CvScalar minScalar = cvScalar(userdata->minScalar0, userdata->minScalar1, userdata->minScalar2);
	CvScalar maxScalar = cvScalar(userdata->maxScalar0, userdata->maxScalar1, userdata->maxScalar2);

	static IplImage *tmp1d = cvCreateImage(cvGetSize(image1), IPL_DEPTH_8U, 1);
	static IplImage *tmp3d = cvCreateImage(cvGetSize(image1), IPL_DEPTH_8U, 3);
	cvCopy(image1, tmp3d, NULL);

	filterByHSV(tmp3d, minScalar, maxScalar, tmp3d);
	
	static int tick = 0;
	static CvRect face = cvRect(0,0,1,1);
	if ((tick %= 10) == 0) {
		//userdata->timestep = 100;
		double scale = 4;
		CvRect *faces = NULL;
		double t = 0;
		int facesCount = findFaces(tmp3d, &faces, scale, &t);
		face = (facesCount != 0) ? faces[0] : face;
		free(faces);
		//printf("%d face(s) detected in %g ms :: 1st face at {%d,%d,%d,%d}", facesCount, t, face.x, face.y, face.width, face.height);
		drawFaces(tmp3d, 1, &face);
	}
	tick++;

	//face extraction
	IplImage *subimage = createSubArray(tmp3d, face);
	cvNamedWindow(CONTROL_WINDOW  "face", 0);
	cvResizeWindow(CONTROL_WINDOW "face", subimage->width, subimage->height);
	cvShowImage(CONTROL_WINDOW    "face", subimage);

	//face histogram
	IplImage *subimage2 = cvCloneImage(subimage);
	cvCvtColor(subimage2, subimage2, CV_BGR2HSV);
	size_t binsCount = 0;
	size_t *bins = NULL;
	//printf("%d (%p)", binsCount, &bins);
	size_t ret = calcularHistograma(subimage2, &binsCount, &bins);
	//printf(" ret=%d %d (%p)", ret, binsCount, bins);
	CvScalar maxValues;
	if (bins) {
		for (size_t i = 0; i < binsCount; i++) {
			size_t idx = 0;
			for (size_t j = 0; j < 256; j++) {
				if (bins[i * 256 + j] > bins[idx]) {
					idx = j;
				}
			}
			maxValues.val[i] = idx;
		}
		free(bins);
	}
#if 0
	if (subimage->width > 10 && subimage->height > 10)
	graficarHistograma(subimage, binsCount, bins);
	cvNamedWindow(CONTROL_WINDOW  "42", 0);
	cvResizeWindow(CONTROL_WINDOW "42", subimage->width, subimage->height);
	cvShowImage(CONTROL_WINDOW    "42", subimage);
#endif

#if 0
	int minH = (int)maxValues.val[0] - 20;
	int maxH = (int)maxValues.val[0] + 20;
	int minS = (int)maxValues.val[1] - 20;
	int maxS = (int)maxValues.val[1] + 20;
	int minV = (int)maxValues.val[2] - 20;
	int maxV = (int)maxValues.val[2] + 20;

	minH = minH < 0 ? 180 - minH : minH;
	maxH = maxH > 180 ? maxH - 180 : maxH;

	printf("%d,%d,%d %d,%d,%d", minH, minS, minV, maxH, maxS, maxV);

	filterByHSV(subimage2, cvScalar(minH, minS, minV, 0), cvScalar(maxH, maxS, maxV, 0), subimage2);
	filterByHSV(subimage2, minScalar, maxScalar, subimage2);
	cvCvtColor(subimage2, subimage2, CV_HSV2BGR);

	cvNamedWindow(CONTROL_WINDOW "41", 0);
	cvResizeWindow(CONTROL_WINDOW "41", subimage2->width, subimage2->height);
	cvShowImage(CONTROL_WINDOW "41", subimage2);
#endif

	cvReleaseImage(&subimage);
	cvReleaseImage(&subimage2);
	
	cvCopy(image1, image2, NULL);
	cvCopy(imageOut, imageOut2, NULL);
	cvCopy(tmp3d, imageOut, NULL);

	//cvReleaseImage(&tmp1d);
	//cvReleaseImage(&tmp3d);

	//afterProcess(userdata);

	printf("\r");
	return 0;
}