Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*-------------------------Grey Scaling-----------------------**/
void utility::addGrey(image& src, image& tgt, ROI roi, int value1, int value2)
{
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++)
{
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0)
value = value1;
else
value = value2;
tgt.setPixel(i, j, src.getPixel(i, j) + value);
if (tgt.getPixel(i, j) > 255)
tgt.setPixel(i, j, 255);
else if (tgt.getPixel(i, j) < 0)
tgt.setPixel(i, j, 0);
}
else {
tgt.setPixel(i, j, src.getPixel(i, j));
}
}
}
/*----------------------------Grey thresholding---------------------------------**/
void utility::binarizeGrey(image& src, image& tgt, ROI roi, int value1, int value2)
{
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0)
value = value1;
else
value = value2;
if (src.getPixel(i, j) > value)
tgt.setPixel(i, j, 255);
else if (src.getPixel(i, j) < value)
tgt.setPixel(i, j, 0);
}
else {
tgt.setPixel(i, j, src.getPixel(i, j));
}
}
}
/*-----------------------------Color thresholding----------------------------------**/
void utility::binarizeColor(image& src, image& tgt, ROI roi, int TC1, int CR1, int CG1, int CB1, int TC2, int CR2, int CG2, int CB2)
{
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int red = src.getPixel(i, j, RED);
int green = src.getPixel(i, j, GREEN);
int blue = src.getPixel(i, j, BLUE);
int TC, CR, CB, CG;
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
TC = TC1;
CR = CR1;
CB = CB1;
CG = CG1;
}
else {
TC = TC2;
CR = CR2;
CB = CB2;
CG = CG2;
}
double temp = sqrt(pow((CR - red), 2) + pow((CG - green), 2) + pow((CB - blue), 2));
if ((int)temp > TC) {
tgt.setPixel(i, j, RED, 256);
tgt.setPixel(i, j, GREEN, 256);
tgt.setPixel(i, j, BLUE, 256);
}
else {
tgt.setPixel(i, j, RED, 0);
tgt.setPixel(i, j, GREEN, 0);
tgt.setPixel(i, j, BLUE, 0);
}
}
else {
tgt.setPixel(i, j, RED, red);
tgt.setPixel(i, j, GREEN, green);
tgt.setPixel(i, j, BLUE, blue);
}
}
}
/*-----------------------------Adaptive gray thresholding----------------------------------**/
void utility::AdaptiveGrayThresh(image& src, image& tgt, ROI roi, int WS1, int W1, int WS2, int W2)
{
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int current = src.getPixel(i, j);
int value = roi.InROI(i, j);
int WS, W;
if (value != -1) {
if (value == 0) {
WS = WS1;
W = W1;
}
else {
WS = WS2;
W = W2;
}
int offset = (WS - 1) / 2;
bool flag = true;
double Sum = 0.0;
for (int k = i - offset; k <= i + offset; k++)
for (int l = j - offset; l <= j + offset; l++) {
if (roi.InROI(k, l) == -1) {
flag = false;
break;
}
Sum = Sum + src.getPixel(k, l);
}
if (flag) {
double Mean = Sum / (pow(WS, 2));
double T = W + Mean;
if (T > current) {
tgt.setPixel(i, j, 256);
}
else {
tgt.setPixel(i, j, 0);
}
}
}
else {
tgt.setPixel(i, j, current);
}
}
}
/*-----------------------------1 D gray smoothing----------------------------------**/
void utility::oneDgraySmoothing(image& src, image& tgt, ROI roi, int WS1, int WS2)
{
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int current = src.getPixel(i, j);
int value = roi.InROI(i, j);
int WS;
if (value != -1) {
if (value == 0) {
WS = WS1;
}
else {
WS = WS2;
}
bool flag = true;
double Sum = 0.0;
int offset = (WS - 1) / 2;
for (int k = i - offset; k <= i + offset; k++)
{
if (roi.InROI(k, j) == -1) {
flag = false;
break;
}
Sum = Sum + src.getPixel(k, j);
}
for (int l = j - offset; l <= j + offset; l++)
{
if (roi.InROI(i, l) == -1) {
flag = false;
break;
}
Sum = Sum + src.getPixel(i, l);
}
if (flag) {
double Mean = Sum / (WS * 2);
tgt.setPixel(i, j, (int)Mean);
}
}
else {
tgt.setPixel(i, j, current);
}
}
}
/*-----------------------------2 D gray smoothing----------------------------------**/
void utility::twoDgraySmoothing(image& src, image& tgt, ROI roi, int WS1, int WS2)
{
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int current = src.getPixel(i, j);
int value = roi.InROI(i, j);
int WS;
if (value != -1) {
if (value == 0) {
WS = WS1;
}
else {
WS = WS2;
}
int offset = (WS - 1) / 2;
bool flag = true;
double Sum = 0.0;
for (int k = i - offset; k <= i + offset; k++)
for (int l = j - offset; l <= j + offset; l++) {
if (roi.InROI(k, l) == -1) {
flag = false;
break;
}
Sum = Sum + src.getPixel(k, l);
}
if (flag) {
double Mean = Sum / (pow(WS, 2));
tgt.setPixel(i, j, (int)Mean);
}
}
else {
tgt.setPixel(i, j, current);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.