ts,ps,mpeg2 decoder and analysis

分析工具,免费下载.

  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  54 随笔 :: 0 文章 :: 168 评论 :: 0 Trackbacks
如何把CM项目中的800*1万的图像,智能切割,防止出现把人头截断,或者一行字给截断了。
智能分析图像中的连续或者轮廓部分,计算空白处,从空白处crop 保存图像。
设计思路如下:
用opencv打开图像,转换为gray, CV_BGR2GRAY,背景灰度,局部二值化或自适应二值化,
阀值和block选择真不容易,再直方图分析和X轴像素影射叠加。再根据zhangqf说的3000就可以分割图片,
求出到双峰图的波谷的y值,目前取了2500,再根据y值cv::Rect( 0, n_last, colNumber, (tmpMatrix[k] - n_last) ),
最后call imwrite存图片。部分code参加我的blog 
http://www.cnitblog.com/dvb-dvb/archive/2019/01/20/91306.html

Mat src = imread(fn);
// imshow("Hello World!",src);
// waitKey(10);
namedWindow("TrackBar",CV_WINDOW_AUTOSIZE);
Mat graydst;
cvtColor(src,graydst,CV_BGR2GRAY);
// = src(src);
//cv::threshold(graydst, g_image_dst, 70, 255, CV_THRESH_BINARY_INV);
cv::adaptiveThreshold(graydst,g_image_dst,255,cv::ADAPTIVE_THRESH_GAUSSIAN_C,cv::THRESH_BINARY,25,10);
//int kkk = 0;
//createTrackbar("Threshold","TrackBar",&kkk,255,on_image_Change,&graydst);
    // threshold(src,g_image_dst,value,255,THRESH_BINARY);
     //Canny(src,dst,value,255);
     imshow("TrackBar",g_image_dst);//  21/22/23这三行代码如果去掉的话,在未调整滑动条的情况下,不会有画面显示
  
//imwrite("E:\\cm.bmp",g_image_dst);
int rowNumber=src.rows;//获取行数
int colNumber=src.cols;//获取列数
int IimageSize =rowNumber;
//计算位图像素部分的大小,也可以是bmpinfoheader.biSizeImage 但它有时候是0,不可靠。  
//IimageSize = WIDTHBYTES(fi->biWidth * fi->biBitCount * 3) * fi->biHeight;
unsigned long * tmpMatrix = new unsigned long[IimageSize];
memset(tmpMatrix, 0, (IimageSize)* sizeof(long));
for(int i=0;i<rowNumber;i++)
{
for (int j = 0; j <colNumber; j++)
{
if(g_image_dst.at<uchar>(i,j) < 128)
tmpMatrix[i] = tmpMatrix[i] + 1;
//if(g_image_dst.at<uchar>(i,j) > 128)
// TRACE(" ");
//else
//{
// TRACE("*");
// tmpMatrix[i] = tmpMatrix[i] + 1;
//}
//TRACE(",%d",g_image_dst.at<uchar>(i,j));
}
TRACE("%4d,%ld\n",i,tmpMatrix[i]);
}
int nMax_line_number = 1024;
int nIndex = 0;
//nLine_numArray
TRACE("-------------------------------\n");
for(int i = 5;i<(IimageSize -5);i++)
{
if(nIndex >= nMax_line_number)
break;
if(tmpMatrix[i-3] == 0 && tmpMatrix[i-2] == 0 && tmpMatrix[i-1] == 0 && tmpMatrix[i] == 0 && tmpMatrix[i+1] == 0 && tmpMatrix[i+2] == 0 && tmpMatrix[i+3] == 0 )
// if(tmpMatrix[i-2] == 0 && tmpMatrix[i-1] == 0 && tmpMatrix[i] == 0 && tmpMatrix[i+1] == 0 && tmpMatrix[i+2] == 0 )
{
nLine_numArray[nIndex] = i;
nIndex++;
TRACE("-%d",i);
i = i + 3;
}
}
TRACE("------------------%d-------------\n",nIndex);
int nCurrent = 2500;
int n_buchang = 0;
int nLoop = 0;
int nReal_cut_num = 0;
int n_min1,m_max2 = 0;
while(nCurrent <= rowNumber)
{
for(int k = nLoop;k<nIndex;k++)
{
if(nCurrent <= nLine_numArray[k])
{
n_min1 = nLine_numArray[k] - nCurrent;
m_max2 = nCurrent - nLine_numArray[k -1];
nLoop = k +1;
if(n_min1 < m_max2)
{
nCurrent = nLine_numArray[k];
}
else
{
nCurrent = nLine_numArray[k -1];
}
tmpMatrix[nReal_cut_num] = nCurrent;
nReal_cut_num ++;
//TRACE("--%d--",nCurrent);
break;
}  
}
nCurrent += 2500;
}
TRACE("--------nReal_cut_num----------%d-------------\n",nReal_cut_num);
int n_last = 0;
for(int k = 0;k<=nReal_cut_num;k++)
{
TRACE("--%d",tmpMatrix[k]);
char str_Filename[MAX_PATH] ="D:\\jza\\ssssss\\1545618538886029.jpg";
sprintf(str_Filename,"e:\\1545618538886029_%d.jpg",(k+1));
cv::Rect tmpRectangle = cv::Rect( 0, n_last, colNumber, (tmpMatrix[k] - n_last) );
if(k ==nReal_cut_num)
{
tmpRectangle = cv::Rect( 0, n_last, colNumber, (rowNumber - n_last) );
}
n_last = tmpMatrix[k];
Mat imageROI = src(tmpRectangle);
imwrite(str_Filename,imageROI);
//CvvImage imageROI; 
//imageROI.CopyOf( src );
//imageROI = image(tmpRectangle);
//
//imwrite("E://ROI111.jpg",imageROI);
}
delete tmpMatrix;
while (char(waitKey(10)) != 'n') 
{
Sleep(1000);
}
destroyWindow("TrackBar");
posted on 2019-01-20 13:47 TS,MPEG2,dvbc专家 阅读(431) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。