Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 放大图像_C++ - Fatal编程技术网

C++ 放大图像

C++ 放大图像,c++,C++,这不会分割错误,但不会读取“原始”文件的像素 Image::scaleUp(int numTimes)常量 { 图像新建图像(宽度*numTimes,高度*numTimes); createImage(宽度*numTimes,高度*numTimes); 对于(int x=0;x

这不会分割错误,但不会读取“原始”文件的像素

Image::scaleUp(int numTimes)常量
{
图像新建图像(宽度*numTimes,高度*numTimes);
createImage(宽度*numTimes,高度*numTimes);
对于(int x=0;x

现在解决*(*除我的图像以B/W表示)

< P> C++中的所有方法都有指向当前对象的指针,名为<代码> < <代码>。可以使用以下内容返回对对象的引用:

 // Note that we're now returning a reference with Image&
 const Image& Image::scaleUp(int numTimes) const
 {
     // ...
     return *this;
 }

您的代码似乎使颜色更亮(将它们乘以
numTimes
)。这就是你想做的吗

如果要返回图像的副本,应在应用转换之前创建副本,然后返回对该图像的引用。也许您的Image类已经有了一个副本构造函数,您可以使用它来获取副本(如果没有,您将不得不添加它或使用另一种方法来构造对象)

我想这就是你想要的:

Image Image::scaleUp(int numTimes) const
{
    Image newImage = new Image(); // You might have a constructor to specify size so data is pre-allocated ? 

    // Your copy-and-scale code, but set data in newImage ....
    // Using copyAll here should be avoided, since it is just copying data 
    // that you will need to set again when doing the scaling.

    return newImage;
}

好的,我想你要做的是应用一个对比度过滤器(每个像素乘以一个值)。如果你想要一个亮度算法,而不是乘法,只需求和。在基本方法上,只要读取每个像素:

float contrast = 1.10; //Apply 10% contrast
for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) 
    {
        int temp = pixelData[x][y] * contrast;
        temp = temp > 255 ? 255 : temp; //it is always good to check if the result is in the range 0..255
        pixelData[x][y] = temp;
    }
 }
float对比度=1.10//应用10%的对比度
对于(int x=0;x255?255:temp;//检查结果是否在0..255范围内总是好的
像素数据[x][y]=温度;
}
}
在上面的代码中,我在原图中原地执行。将对每个像素执行这些步骤。我还假设每个通道(RGB)有8位像素(0..255)


我还假设x(列)指向RGB空间的一个元素,比如R,而x+1指向G,x+2指向B。这种情况下,所有像素通道在内存中都是相邻的,按比例放大是指更改图像大小吗?您当前正在做的是缩放颜色级别(变大或变暗)图像是你想要的吗?呃,不是。我需要通过numTimes缩放整个图像。如何访问大小参数以使图像变大?@codefail:那么您是否在寻找图像大小调整算法(如最近邻、双线性、双三次等)?我怀疑这里的任何人都不会为你的家庭作业实现一个链接,但如果你明确表示这是你想要的,我相信你至少会得到一些好的链接。@Jefromi:不,对不起,这对我来说是一个很难问的项目,因为涉及的文件太多了(我正在处理2.cpp文件,1.cpp文件是主程序,2.h文件)所有这些几乎都是相互依赖的。我迷路了,因为我不知道如何访问处理“原始图像”大小的函数/对象,我需要获取创建的原始图像,复制它,然后应用numTimes(图像要按比例放大的值,在mainprogram.cpp中从main调用)但是我不能这样做,因为原型是预定义的。如果方法是
const
,那么
this
就是
const-Image*
,所以你不能返回
Image&
,只能返回
const-Image&
。只是一个一般性的问题。伙计们:如果问题失控(像这样),我会发布一个新问题吗?就这样,我走神了,开始对图像进行灰度处理。我对那个张照片有疑问,你们是对的。用复制功能编辑文章我没有,我犯了一个错误。事实上,我正在尝试增加图像的大小,而不是任何颜色/
float contrast = 1.10; //Apply 10% contrast
for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) 
    {
        int temp = pixelData[x][y] * contrast;
        temp = temp > 255 ? 255 : temp; //it is always good to check if the result is in the range 0..255
        pixelData[x][y] = temp;
    }
 }