BlockHoughDetector::BlockHoughDetector(cv::Mat img) { if (img.channels() > 1) cv::cvtColor(img, img, CV_BGR2GRAY); cv::Mat left = img.clone()(cv::Rect(100, 0, 400, img.rows)); //vector<int> o; //o.push_back(-1); //o.push_back(-2); //o.push_back(-2); //o.push_back(-2); //o.push_back(0);//0点表示 //o.push_back(1); //o.push_back(1); //o.push_back(1); //o.push_back(1); //o.push_back(1); //o.push_back(1); //o.push_back(1); //Caculte(left, o); //char o[] = { // -5, 0, 0, 1, 1, 1, 1, // -5, 0, 0, 1, 1, 1, 1, // -5, 0, 0, 1, 1, 1, 1 }; char o[] = { -5, 0, 0, 1, 1, 1, 1 ,1,1,1}; cv::Point center = cv::Point(3, 2); cv::Mat _operator(3, 7, CV_8S); InitMat(_operator, o); InitMat(_operator, o); Caculte(left, _operator, center); }
Mat productMat (const Mat * pMatA, const Mat * pMatB) { int n, m, iA, iB, jA, jB, k; double value, VA, VB; Mat pResult; //创建结果稀疏矩阵指针 iA = (*pMatA)->Ni; jA = (*pMatA)->Nj; iB = (*pMatB)->Ni; jB = (*pMatB)->Nj; InitMat (&pResult, iA, jB); if (jA == iB) { for ( n = 1; n <= iA; n++ ) { for ( m = 1; m <= jB; m++ ) { /* 计算C[n m]的value C[n m]=A[n 1]*B[1 m]+A[n 2]*B[2 m]+...+A[n k]*B[k m] k = jA = iB */ for ( k = 1, value = 0; k <= jA; k++ ) { //元素的值有一个为0则二者相乘直接为0 if ( ((VA = findElemValue (pMatA, n, k)) == 0) || ((VB = findElemValue (pMatB, k, m)) == 0) ) { value += 0; } //二者都不为0,则计算其乘积并进行累加 else { value += VA*VB; //+=运算前要记得先对value进行初始化 } } if ( value != 0 ) addElement (value, n, m, &pResult); else continue; } } return pResult; } else { printf ("productMat: Matrix dimension mismatch!!\n"); return pResult; //此处返回的矩阵不含有非0元素,但其指针不为NULL } }
Mat productK (const Mat * pMatA, double K) { Elem * pCurrent; Mat pResult; if ( *pMatA != NULL && K !=0 ) //检测矩阵指针是否为空 { pCurrent = (*pMatA)->HEAD; InitMat (&pResult, (*pMatA)->Ni, (*pMatA)->Nj); while ( pCurrent != NULL ) { addElement (K*pCurrent->VA, pCurrent->IA, pCurrent->JA, &pResult); pCurrent = pCurrent->NEXT; } return pResult; } else { printf ("productK: Matix is Empty!!\n "); InitMat (&pResult, 1, 1); return pResult; //此处返回的矩阵不含有非0元素,但其指针不为NULL } }
Mat addMat (const Mat * pMatA, const Mat * pMatB) { int n, m, iA, iB, jA, jB; double value, VA, VB; Mat pResult; //创建结果稀疏矩阵指针 iA = (*pMatA)->Ni; jA = (*pMatA)->Nj; iB = (*pMatB)->Ni; jB = (*pMatB)->Nj; InitMat (&pResult, iA, jB); if ( iA == iB && jA == jB ) { for ( n = 1; n <= iA; n++ ) { for ( m = 1, value = 0; m <= jA; m++ ) { /* 计算C[n m]的value C[n m]=A[n m]+B[n m] */ if ( ((VA = findElemValue (pMatA, n, m)) == 0) & ((VB = findElemValue (pMatB, n, m)) == 0) ) //不要用&&,其具有短路性质,导致另一个表达式没有计算 { value = 0; } //二者都不为0 else { value = VA + VB; //运算前要记得先对value进行初始化 } if ( value != 0 ) addElement (value, n, m, &pResult); else continue; } } return pResult; } else { printf ("addMat: Matrix dimension mismatch!!\n"); return pResult; //此处返回的矩阵不含有非0元素,但其指针不为NULL } }
/* 构造函数*/ RunLength::RunLength(Mat image_input){ rows = image_input.rows; cols = image_input.cols; image_in = image_input; double factor = sqrt(nPIXELS / (cols*rows)); ncols = cvRound(cols*factor); nrows = cvRound(rows*factor); width_height = Mat(1, 4, CV_32F); float width_height_0[4] = { ncols, ncols, nrows, nrows }; for (int i = 0; i < width_height.cols; i++) width_height.at<float>(0, i) = *(width_height_0 + i); Pyramid = Mat(21, 4, CV_32F); InitMat(Pyramid, pyramid); }
Mat TransposeMat (const Mat * pMatA) { Elem * pCurrent; Mat pResult; InitMat (&pResult, (*pMatA)->Nj, (*pMatA)->Ni); if ( *pMatA != NULL ) { pCurrent = (*pMatA)->HEAD; while ( pCurrent != NULL) { addElement (pCurrent->VA, pCurrent->JA, pCurrent->IA, &pResult); pCurrent = pCurrent->NEXT; } return pResult; } else { printf ("TransposeMat: Matix is Empty!!\n "); return pResult; } }
Mat solveEqs (LDU * factorTable, Mat * pMat) { //factorTable Mat L, D, U; //pMat int n; Elem * pCurrentB; //result: factorTable * result = B Mat result; //检测因子表指针是否为空 if ( factorTable == NULL ) { printf ("solveEqs:The point of factorTable is empty!!\n"); return NULL; } //检测因子表中LDU矩阵是否为空 U = (*factorTable)->matU; D = (*factorTable)->matD; L = (*factorTable)->matL; if ( MatIsEmpty (&L) || MatIsEmpty (&D) || MatIsEmpty (&U) ) { printf ("solveEqs:There is an empty Matix in the factorTable!!\n"); return NULL; } //检测pMat指针是否为空 if ( pMat == NULL ) { printf ("solveEqs: This is an empty pMat!!\n"); return NULL; } //检测矩阵是否为空 else if ( MatIsEmpty (pMat) ) { printf ("solveEqs: Matix is empty!!\n"); return NULL; } else { n = (*pMat)->Ni; pCurrentB = (*pMat)->HEAD; } //初始化Mat result InitMat (&result, n, 1); //复制pMat->result while ( pCurrentB != NULL ) { addElement (pCurrentB->VA, pCurrentB->IA, pCurrentB->JA, &result); pCurrentB = pCurrentB->NEXT; } //利用L矩阵进行前代运算 int i, k; double Dk, Lik, Bk, Bi, Uik; for ( k = 1; k <= n - 1; k++ ) { if ( (Bk = findElemValue (&result, k, 1)) != 0 ) { for ( i = k + 1; i <= n; i++ ) { if ( (Lik = findElemValue (&L, i, k)) !=0 ) { Bi = findElemValue (&result, i, 1); Bi = Bi - Lik*Bk; if ( Bi == 0 ) { if ( findElemValue (&result, i, 1) != 0 ) { removeElement (&result, i, 1); } } else { updateElement (Bi, i, 1, &result); } } } } } //利用D矩阵进行除法运算 for ( k = 1; k <= n; k++ ) { if ( (Dk = findElemValue (&D, k, k)) != 0 ) { if ( (Bk = findElemValue (&result, k, 1)) != 0) { Bk = Bk / Dk; updateElement (Bk, k, 1, &result); } else continue; } else { printf ("solveEqs: D[%d %d] is zero!!\n"); return NULL; } } //showMat (&result); //利用U矩阵进行回代运算 for ( k = n; k >= 2; k-- ) { if ( (Bk = findElemValue (&result, k, 1)) != 0 ) { for ( i = k - 1; i >= 1; i-- ) { if ( (Uik = findElemValue (&U, i, k)) != 0 ) { Bi = findElemValue (&result, i, 1); Bi = Bi - Uik*Bk; if ( Bi == 0 ) { if ( findElemValue (&result, i, 1) != 0 ) { removeElement (&result, i, 1); } } else { updateElement (Bi, i, 1, &result); } } } } else continue; } return result; }
LDU CalFactorT (const Mat * pMat) { int n, m, i, j, p; double Vpj1, Vpj2, Vpj, Vpp, Vij1, Vij2, Vij, Vip; Elem * pCurrent; //检测pMat指针是否为空 if ( pMat == NULL ) { printf ("CalFactorT: This is an empty pMat!!\n"); return NULL; } //检测矩阵是否为空 else if (MatIsEmpty (pMat)) { printf ("CalFactorT: Matix is empty!!\n"); return NULL; } else { n = (*pMat)->Ni; m = (*pMat)->Nj; if ( n != m ) { printf ("CalFactorT: Matix is [%d %d], can't calculate FactorTable!!\n", n, m); return NULL; } else { //初始化LDU因子表指针及其指向的因子表 LDU factorTable; factorTable = (matLDU *)malloc (sizeof(matLDU)); Mat matL, matD, matU; //初始化因子表中的稀疏矩阵 InitMat (&matL, n, m); InitMat (&matD, n, m); InitMat (&matU, n, m); //对LDU因子表中个稀疏矩阵进行赋值 factorTable->matL = matL; factorTable->matD = matD; factorTable->matU = matU; //因子表中L、D、U矩阵求解及赋值 //复制矩阵*pMat到matD pCurrent = (*pMat)->HEAD; while (pCurrent != NULL) { addElement (pCurrent->VA, pCurrent->IA, pCurrent->JA, &matD); pCurrent = pCurrent->NEXT; } //求FactorTable for ( p = 1; p <= n - 1; p++ ) { for ( j = p + 1; j <= m; j++ ) { if ( (Vpp = findElemValue (&matD, p, p)) != 0 ) { if ( (Vpj1 = findElemValue (&matD, p, j)) != 0 ) { Vpj2 = Vpj1 / Vpp; updateElement (Vpj2, p, j, &matD); } } else printf ("CalFactorTError: A[%d %d]=0, Division by zero!!\n", p, p); for ( i = p + 1; i <= n; i++ ) { if ( ((Vpj = findElemValue (&matD, p, j))== 0) | ((Vip = findElemValue (&matD, i, p)) == 0) ) { } else { Vij1 = findElemValue (&matD, i, j); Vij2 = Vij1 - Vpj*Vip; if ( (Vij2 != 0) & (Vij1 != 0) ) { updateElement (Vij2, i, j, &matD); } else if ( (Vij2 == 0) & (Vij1 != 0) ) { removeElement (&matD, i, j); } else if ( (Vij2 != 0) & (Vij1 == 0) ) { addElement (Vij2, i, j, &matD); } else { } } } } } //showMat (&matD); //分别求解matU,matL,matD //追加matL与matU矩阵元素 pCurrent = matD->HEAD; while (pCurrent != NULL) { i = pCurrent->IA; j = pCurrent->JA; Vij = pCurrent->VA; if ( i > j ) { Vij = Vij / findElemValue (&matD, j, j); addElement (Vij, i, j, &matL); } else if ( i < j ) { addElement (Vij, i, j, &matU); } else { addElement (1, i, j, &matU); addElement (1, i, j, &matL); } pCurrent = pCurrent->NEXT; } //matD矩阵单位化(删除非对角线元素) pCurrent = matD->HEAD; while (pCurrent != NULL) { i = pCurrent->IA; j = pCurrent->JA; if ( i != j ) { pCurrent = pCurrent->NEXT; removeElement (&matD, i, j); } else pCurrent = pCurrent->NEXT; } return factorTable; } } }
int main(int argc, char *argv[]) { printf("WG size of kernel 1 = %d, WG size of kernel 2= %d X %d\n", BLOCK_SIZE_0, BLOCK_SIZE_1_X, BLOCK_SIZE_1_Y); float *a=NULL, *b=NULL, *finalVec=NULL; float *m=NULL; int size = -1; FILE *fp; // args char filename[200]; int quiet=1,timing=0,platform=-1,device=-1; // parse command line if (parseCommandline(argc, argv, filename, &quiet, &timing, &platform, &device, &size)) { printUsage(); return 0; } context = cl_init_context(platform,device,quiet); if(size < 1) { fp = fopen(filename, "r"); fscanf(fp, "%d", &size); a = (float *) malloc(size * size * sizeof(float)); InitMat(fp,size, a, size, size); b = (float *) malloc(size * sizeof(float)); InitAry(fp, b, size); fclose(fp); } else { printf("create input internally before create, size = %d \n", size); a = (float *) malloc(size * size * sizeof(float)); create_matrix(a, size); b = (float *) malloc(size * sizeof(float)); for (int i =0; i< size; i++) b[i]=1.0; } if (!quiet) { printf("The input matrix a is:\n"); PrintMat(a, size, size, size); printf("The input array b is:\n"); PrintAry(b, size); } // create the solution matrix m = (float *) malloc(size * size * sizeof(float)); // create a new vector to hold the final answer finalVec = (float *) malloc(size * sizeof(float)); InitPerRun(size,m); //begin timing // printf("The result of array b is before run: \n"); // PrintAry(b, size); // run kernels ForwardSub(context,a,b,m,size,timing); // printf("The result of array b is after run: \n"); // PrintAry(b, size); DIVIDEND_CL_WRAP(clFinish)(command_queue); //end timing if (!quiet) { printf("The result of matrix m is: \n"); PrintMat(m, size, size, size); printf("The result of matrix a is: \n"); PrintMat(a, size, size, size); printf("The result of array b is: \n"); PrintAry(b, size); BackSub(a,b,finalVec,size); printf("The final solution is: \n"); PrintAry(finalVec,size); } free(m); free(a); free(b); free(finalVec); cl_cleanup(); //OpenClGaussianElimination(context,timing); return 0; }