![](/img/trans.png)
[英]JavaCV FFmpegFrameGrabber & Java2DFrameConverter creating weird looking image
[英]Creating image with javacv
我想創建一個空白圖像,這意味着一個白色背景圖像,其中一個已經從另一個圖像中選擇了一些圖像,我想將這些圖像添加到創建的空圖像中,我可以使用javacv來應用它嗎?
這是我的代碼的一部分,用於從已加載的圖像中選擇圖像
IplImage originalImage = cvLoadImage("test/test_11.jpg");
// We need a grayscale image in order to do the recognition, so we
// create a new image of the same size as the original one.
IplImage resultImage = IplImage.create(originalImage.width(),
originalImage.height(), IPL_DEPTH_8U, 1);
/* convert RGB image we loaded into an grey image */
cvCvtColor(originalImage, resultImage , CV_BGR2GRAY);
cvSaveImage("test/test_12.jpg", cvtColorImage);
/* smooth(blur) the above image, using gaussian blur method */
cvSmooth(resultImage , resultImage , CV_GAUSSIAN, 7);
/*
* Then we can threshold the blurred image. Instead of normal
* thresholding i've used adaptive thresholding for better results. In
* adaptive thresholding it calculates threshold values for each pixel
* on image by looking at pixels surrounding that pixel.
*/
cvAdaptiveThreshold(resultImage , resultImage , 255,
CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, 11, 5);
/*
* Then comes the most important part. It is finding contours in
* thresholded image. Code given below will find all the contours in the
* above image and store them to dynamic data structure CvSeq contours.
* Function cvFindContours() will do the job for us and it will return
* the number of contours detected in the image.
*/
CvMemStorage storage = CvMemStorage.create();
CvSeq contours = new CvContour(null);
int noOfContors = cvFindContours(resultImage , storage, contours,
Loader.sizeof(CvContour.class), CV_RETR_CCOMP,
CV_CHAIN_APPROX_NONE, new CvPoint(0, 0));
/*
* If we use the above image, function will detect x contours in above
* image. Then we have to iterate through the data structure and filter
* out objects we need. So i used a little trick here. I ignored
* contours with bounding box with area greater than 1200 and less than
* 3000. This will leave 10 contours out of x.
*/
CvSeq ptr = new CvSeq();
int count = 1;
Random rand = new Random();
CvPoint p1 = new CvPoint(0, 0), p2 = new CvPoint(0, 0);
for (ptr = contours; ptr != null; ptr = ptr.h_next()) {
Color randomColor = new Color(rand.nextFloat(), rand.nextFloat(),
rand.nextFloat());
CvScalar color = CV_RGB(randomColor.getRed(),
randomColor.getGreen(), randomColor.getBlue());
CvRect sq = cvBoundingRect(ptr, 0);
double prop = (double) (sq.width()) / sq.height();
if (sq.width() > 15)
if (prop > 0 && prop < 3) {
System.out.println(sq.width() + " " + sq.height() + " "
+ prop);
p1.x(sq.x());
p2.x(sq.x() + sq.width());
p1.y(sq.y());
p2.y(sq.y() + sq.height());
cvRectangle(resultImage , p1, p2, CV_RGB(255, 0, 0), 2, 8,
0);
// cvDrawContours(resultImage , ptr, color, CV_RGB(0, 0, 0),
// -1, CV_FILLED, 8, cvPoint(0, 0));
count++;
}
}
System.out.println("Count =" + (count - 1));
cvSaveImage("test/test_16.jpg", resultImage );
}
通過循環中的此代碼,我正在獲取圖像列表,我想將這些圖像添加到新的空圖像中
據我了解,也許您想使用
IplImage sumImage = IplImage.create(originalImage.width(),
originalImage.height(), IPL_DEPTH_8U, 1);
並將內容設置為零
cvSetZero(sumImage); //Adding to black Image may be correct than white image.
然后用
cvAdd(sumImage, resultImage, sumImage) in your for loop.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.