1

及时我正在写一个小seam-carving应用程序

我的问题是以有效的方式从图像中删除检测到的“接缝”。

我有以下代码:

    private void removePathFromImageV(CvMat img, int[] path){

    int band = 3;

    double ch = 0.0;
        for (int y = 0; y < img.rows()-1; ++y){
            for (int x = path[y]-1; x < img.cols()-1; ++x){

                for (int b = 0; b < band; ++b){
                ch = img.get(y,x+1,b);
                img.put(y,x,b,ch);
            }
        }
    }

}

有没有将元素从 path[y]-1 转移到 img.cols()-1 的选项?

问候

4

1 回答 1

2

您的问题是您必须为每一行抑制不同位置的像素,并且具有执行此操作的图像结构效率不高,因为您必须在删除的像素之后移动所有像素。您可以尝试将整个图像转换为对删除操作有效的数据结构,例如在 C++ 中std::deque,一个带有随机访问迭代器的双端队列。然后,您可以轻松地抑制每一行中的元素。最后,您可以从结构复制回正确的图像。

这是C++中的想法

// assuming image is a CV_8UC3 image
std::deque<std::deque<cv::Vec3b> > pixelsDeq(image.rows, std::deque<cv::Vec3b>(image.cols));
for (int i = 0; i < image.rows; ++i)
  for (int j = 0; j < image.cols; ++j)
    pixelsDeq[i][j] = image.at<cv::Vec3b>(i, j);

// then remove pixels from the path (remove sequentially for all the paths you probably have)
  for (int j = 0; j < image.rows; ++j) {
    pixelsDeq[j].erase(pixelsDeq[j].begin() + paths[j]);


// at the end, copy back to a proper image
cv::Mat output = cv::Mat::zeros(pixelsDeq.size(), pixelsDeq[0].size(), CV_8UC3);
for (int i = 0; i < output.rows; ++i) {
  for (int j = 0; j < output.cols; ++j) {
    output.at<cv::Vec3b>(i,j) = pixelsDeq[i][j];
  } 
}
于 2012-10-17T09:45:00.743 回答