RGB格式的图片如何转换成yuv

#include
#include
#include
#include
using namespace std;
using namespace cv;

int main()
{
ifstream fin("raw.txt");
if (!fin)
cout << "err read file" << endl;
vector result;
int tmp;
while (fin >> tmp)
{
result.push_back(tmp);
}
cout << result.size() << "   " << result.size() / 800 << endl;
Mat dst(480, 800, CV_8UC3);//3 channels
for (int i = 0;i<480 * 2;i += 2)
for (int j = 0;j < 800 * 2;j += 2)
{
dst.at(i/2, j/2)[0] = result.at(i * 800*2 + 800 * 2 + j + 1);//r
dst.at(i/2, j/2)[1] = (result.at(i * 800*2 + j + 1) + result.at(i * 800*2 + 800*2 + j)) / 2;//g
dst.at(i/2, j/2)[2] = result.at(i * 800*2  + j );//b
}
#if 0
for (int i = 0; i < 480; i++) 
{
for (int j = 0; j < 800; j++)
{
if (j % 2)
{
dst.at(i, j)[2] = result.at(2 * i * 480 * 2 + 2 * j + 1);//r
dst.at(i, j)[1] = result.at(2 * i * 480 * 2 + 2 * j) + result.at((2 * i + 1) * 480 * 2 + 2 * j + 1);//g
dst.at(i, j)[0] = result.at((2 * i + 1) * 480 * 2 + 2 * j);//b
}
else
{
dst.at(i, j)[0] = result.at(2 * i * 480 * 2 + 2 * j + 1);//b
dst.at(i, j)[1] = result.at(2 * i * 480 * 2 + 2 * j) + result.at((2 * i + 1) * 480 * 2 + 2 * j + 1);//g
dst.at(i, j)[2] = result.at((2 * i + 1) * 480 * 2 + 2 * j);//r
}
if (j % 2)
{
dst.at(i, j)[2] = result.at(2 * i * 800 * 2 + 2 * j);//r
dst.at(i, j)[1] = (result.at(2 * i * 800 * 2 + 2 * j + 1) + result.at((2 * i + 1) * 800 * 2 + 2 * j)) / 2;//g
dst.at(i, j)[0] = result.at((2 * i + 1) * 800 * 2 + 2 * j + 1);//b
}
else
{
dst.at(i, j)[0] = result.at(2 * i * 800 * 2 + 2 * j);//b
dst.at(i, j)[1] = (result.at(2 * i * 800 * 2 + 2 * j + 1) + result.at((2 * i + 1) * 800 * 2 + 2 * j)) / 2;//g
dst.at(i, j)[2] = result.at((2 * i + 1) * 800 * 2 + 2 * j + 1);//r
}
if (j % 2)
{
dst.at(i, j)[2] = result.at(2 * i * 800 * 2 + 2 * j);//r
dst.at(i, j)[1] = (result.at(2 * i * 800 * 2 + 2 * j + 1) + result.at((2 * i + 1) * 800 * 2 + 2 * j)) / 2;//g
dst.at(i, j)[0] = result.at((2 * i + 1) * 800 * 2 + 2 * j + 1);//b
}
else
{
dst.at(i, j)[0] = result.at(2 * i * 800 * 2 + 2 * j);//b
dst.at(i, j)[1] = (result.at(2 * i * 800 * 2 + 2 * j + 1) + result.at((2 * i + 1) * 800 * 2 + 2 * j)) / 2;//g
dst.at(i, j)[2] = result.at((2 * i + 1) * 800 * 2 + 2 * j + 1);//r
}

if (j % 2)
{
dst.at(i, j)[0] = result.at(2 * i * 800 * 2 + 2 * j);//r
dst.at(i, j)[1] = (result.at(2 * i * 800 * 2 + 2 * j + 1) + result.at((2 * i + 1) * 800 * 2 + 2 * j)) / 2;//g
dst.at(i, j)[2] = result.at((2 * i + 1) * 800 * 2 + 2 * j + 1);//b
}
else
{
dst.at(i, j)[2] = result.at(2 * i * 800 * 2 + 2 * j);//b
dst.at(i, j)[1] = (result.at(2 * i * 800 * 2 + 2 * j + 1) + result.at((2 * i + 1) * 800 * 2 + 2 * j)) / 2;//g
dst.at(i, j)[0] = result.at((2 * i + 1) * 800 * 2 + 2 * j + 1);//r
}
       }
}
#endif
imshow("Dst", dst);
cvWaitKey();
}

//============================================================================================================
#include
#include
#include
#include
using namespace std;
using namespace cv;

int main()
{
ifstream fin("raw1.txt");
if (!fin)
cout << "err read file" << endl;
vector result;
int tmp;
while (fin >> tmp)
{
result.push_back(tmp);
}
cout << result.size() << "   " << result.size() / 800 << endl;
/*WIDTH=800*2,HEIGHT=480*2
B G
G R
*/
Mat dst_B(800, 480, CV_8UC1);
int here, other;
for (int j = 0; j < 480; j++)
{
for (int i = 0; i < 800; i++)
{
here = j * 800 * 4 + i * 2;
//here = j * 800 * 4 + i * 4;
dst_B.at(i, j) = result.at(here);
}
}

Mat dst_R(800, 480, CV_8UC1);
for (int j = 0; j < 480; j++)
{
for (int i = 0; i < 800; i++)
{
here = 800 * 2 + j * 800 * 4 + i * 2 + 1;
dst_R.at(i, j) = result.at(here);
}
}


Mat dst_G(800, 480, CV_8UC1);
for (int j = 0; j < 480; j++)
{
for (int i = 0; i < 800; i++)
{
here = j * 800 * 4 + i * 2 + 1;
other = 800 * 2 + j * 800 * 4 + i * 2;
dst_G.at(i, j) = (result.at(here) + result.at(other)) / 2;
}
}

Mat rgb(800, 480, CV_8UC3);
for (int i = 0; i < 800; i++)
for (int j = 0; j < 480; j++)
{
rgb.at(i, j)[0] = dst_R.at(i, j);
rgb.at(i, j)[1] = dst_G.at(i, j);
rgb.at(i, j)[2] = dst_B.at(i, j);
}

imshow("Dst", rgb);
imwrite("Dst.bmp", rgb);
cvWaitKey();

}



你可能感兴趣的:(camera)