/下面代码包含了图片的灰度,二值化,边缘检测,放大图片,切割图片的功能。但在我们实际项目中只要做图片的灰度和边缘检测, convertToGrayscale作用就是确定实际图片的大小(切图片的作用)
//.h 文件
{
int _nOrignX;
int _nOrignY;
int _nWidth;
int _nHeight;
}
//.m 文件
- (void)getRectImage:(UIImage*)img{
CGSize size = [img size];
int width = size.width;
int height = size.height;
// the pixels will be painted to this array
uint32_t *mypixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));
// clear the pixels so any transparency is preserved
memset(mypixels, 0, width * height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// create a context with RGBA pixels
CGContextRef context = CGBitmapContextCreate(mypixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
// paint the bitmap to our context which will fill in the pixels array
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [img CGImage]);
int tt = 1;
CGFloat intensity;
bool bFind = NO;
int nOrignX = 0;
int nOrignY = 0;
int nWidth = width;
int nHeight = height;
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t *rgbaPixel = (uint8_t *) &mypixels[y * width + x];
intensity = (rgbaPixel[tt] + rgbaPixel[tt + 1] + rgbaPixel[tt + 2]) / 3. / 255.;
if ((int)intensity == 1) {
bFind = YES;
nOrignX = x;
nOrignY = y;
break;
}
}
if (bFind ) {
break;
}
}
//NSLog(@"---fOrignX:%d .....fOrignY:%d...width:%d..height:%d..",nOrignX,nOrignY,width,height);
for(int x = width; x > nOrignX; x--) {
uint8_t *rgbaPixel = (uint8_t *) &mypixels[(nOrignY + 5) * width + x];
intensity = (rgbaPixel[tt] + rgbaPixel[tt + 1] + rgbaPixel[tt + 2]) / 3. / 255.;
if ((int)intensity == 1) {
nWidth = x - nOrignX;
break;
}
}
for(int y = height; y >nOrignY; y--) {
uint8_t *rgbaPixel = (uint8_t *) &mypixels[y * width + nOrignX];
intensity = (rgbaPixel[tt] + rgbaPixel[tt + 1] + rgbaPixel[tt + 2]) / 3. / 255.;
if ((int)intensity == 1) {
nHeight = y - nOrignY;
break;
}
}
free(mypixels);
_nOrignX = nOrignX;
_nOrignY = nOrignY;
_nWidth = nWidth;
_nHeight = nHeight;
NSLog(@"----x:%d.y:%d.width:%d.height:%d",_nOrignX,_nOrignY,_nWidth,_nHeight);
}
- (void)test {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];// Create grayscale IplImage from UIImage
//灰度化
IplImage *img_color = [self CreateIplImageFromUIImage:imageView.image];
IplImage *img = cvCreateImage(cvGetSize(img_color), IPL_DEPTH_8U, 1);
cvCvtColor(img_color, img, CV_BGR2GRAY);
cvReleaseImage(&img_color);
/* //二值化
IplImage *img2 = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
cvThreshold(img,img2,10.0,255,CV_THRESH_OTSU);
//cvThreshold(img,img2,128.0,255,CV_THRESH_OTSU);
cvReleaseImage(&img);*/
// Detect edge
IplImage *img2 = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
cvCanny(img, img2, 64, 128, 3);
cvReleaseImage(&img);
/*// Scaling down 放大图片
IplImage *img2 = cvCreateImage(cvSize(img_color->width/2,img_color->height/2), IPL_DEPTH_8U, 3);
cvPyrDown(img_color, img2, CV_GAUSSIAN_5x5);
cvReleaseImage(&img_color);*/
/*//高斯
IplImage *img2 = cvCreateImage(cvGetSize(img5),IPL_DEPTH_8U, 1);
cvSmooth(img5, img2,CV_GAUSSIAN,3,0,0,0);
cvReleaseImage(&img5);*/
// Convert black and whilte to 24bit image then convert to UIImage to show
IplImage *image = cvCreateImage(cvGetSize(img2), IPL_DEPTH_8U, 3);
for(int y=0; y<img2->height; y++) {
for(int x=0; x<img2->width; x++) {
char *p = image->imageData + y * image->widthStep + x * 3;
*p = *(p+1) = *(p+2) = img2->imageData[y * img2->widthStep + x];
}
}
cvReleaseImage(&img2);
[self getRectImage:[self UIImageFromIplImage:image]];
/*
图像的剪切有多种方法,其中一种是使用ROI的方法
第一步:将需要剪切的图像图像不部分设置为ROI
cvSetImageROI(src , cvRect(x,y,width,height));
第二步:新建一个与需要剪切的图像部分同样大小的新图像
cvCreateImage(cvSize(width,height),IPL_DEPTH,nchannels);
第三步:将源图像复制到新建的图像中
cvCopy(src,dst,0);
第四步:释放ROI区域
cvResetIamgeROI(src);
*/
IplImage *img_color11 = [self CreateIplImageFromUIImage:imageView.image];
cvSetImageROI(img_color11 , cvRect(_nOrignX,_nOrignY,_nWidth,_nHeight));
IplImage *image4 = cvCreateImage(cvSize(_nWidth,_nHeight), IPL_DEPTH_8U, 3);
cvCopy(img_color11, image4, 0);
imageView.image = [self UIImageFromIplImage:image4];
cvReleaseImage(&img_color11);
cvReleaseImage(&image);
[pool release];
}