Function with changing labels - c#

I am building a function in C# that will slice a frame and then count the white pixels in that slice. I want to be able to uses this function on multiple frames/pictureBoxs, so I need to change the name of label.Text to the correct label. How should I go about this? I'll be using mergedImage, mergedImage2, and BinaryImage. Each Image will also have outerRight, innerRight, Cent, innerLeft, outerLeft.
private void Slicing(Mat inputFrame)
{
Mat frame = inputFrame.Clone();
//count the number of white pixels
int whitePixelsExtendedOutterLeft = 0;
int whitePixelsExtendedInnerLeft = 0;
int whitePixelsExtendedCent = 0;
int whitePixelsExtendedInnerRight = 0;
int whitePixelsExtendedOutterRight = 0;
Image<Gray, byte> img = frame.ToImage<Gray, byte>();
//Calculates the outter left slice
for (int i = 0; i <= 4; i++)
{
for (int x = i; (frame.Width / 5) * i < (frame.Width / 5) * (i+1) ; x++)
{
for (int y = 0; y < frame.Height; y++)
{
switch (i)
{
case 0:
if (img.Data[y, x, 0] == 255) whitePixelsExtendedOutterLeft++;
break;
case 1:
if (img.Data[y, x, 0] == 255) whitePixelsExtendedInnerLeft++;
break;
case 2:
if (img.Data[y, x, 0] == 255) whitePixelsExtendedCent++;
break;
case 3:
if (img.Data[y, x, 0] == 255) whitePixelsExtendedInnerRight++;
break;
case 4:
if (img.Data[y, x, 0] == 255) whitePixelsExtendedOutterRight++;
break;
}
}
}
}
//displays their respective white pixel count to form1
Invoke(new Action(() =>
{
mergedImage.Text = $"{whitePixelsExtendedOutterLeft} Outter Left ";
//whitePixelsExtendedOutputLIL.Text = $"{whitePixelsExtendedInnerLeft} Inner Left ";
//whitePixelsExtendedOutputLCent.Text = $"{whitePixelsExtendedCent} Center ";
//whitePixelsExtendedOutputLIR.Text = $"{whitePixelsExtendedInnerRight} Inner Right ";
//whitePixelsExtendedOutputLOR.Text = $"{whitePixelsExtendedOutterRight} Outter Right ";
}));
}
I have tried concatenation of the of the label like: "mergedImage" ++ i .Text . of course this did not work.

Related

I'm trying to do edge detection. But it works on just one image

I'm trying to create a simple edge detection filter. And as I said it works with only one image. I'm trying to create this filter with 2 steps.
Blurring image (with meanfilter)
Calculate ( Original image-Blurring image)
The first step works well. And code of second one is simple like first one. But I see an error message:
System.ArgumentOutOfRangeException: 'Parameter must be positive and < Height.
Parameter name: y'
Working image:https://i.hizliresim.com/dLXkbn.png
My code:
public void edgedetectionfilter( )
{
Bitmap InputPicture,BlurredPicture, OutputPicture;
InputPicture = new Bitmap(pBox_SOURCE.Image);
BlurredPicture = new Bitmap(pBox_PROCESSED.Image);
int PicWidth = InputPicture.Width;
int PicHeight= InputPicture.Height;
OutputPicture = new Bitmap(PicWidth, PicHeight);
OutputPicture = InputPicture;
int x, y, difR, difG, difB;
Color OrgPicColoValue,BluredPicColorValue;
for (x = 0; x < PicWidth; x++)
{
for (y = 0; y < PicWidth; y++)
{
BluredPicColorValue = BlurredPicture.GetPixel(x, y);
OrgPicColoValue = InputPicture.GetPixel(x, y); //ERROR LINE
difR = Convert.ToInt16(OrgPicColoValue.R -BluredPicColorValue.R);
difG = Convert.ToInt16(OrgPicColoValue.G- BluredPicColorValue.G );
difB = Convert.ToInt16(OrgPicColoValue.B- BluredPicColorValue.B);
if (difR > 255) difR = 255;
if (difG > 255) difG = 255;
if (difB > 255) difB = 255;
if (difR < 0) difR = 0;
if (difG < 0) difG = 0;
if (difB < 0) difB = 0;
OutputPicture.SetPixel(x, y, Color.FromArgb(difR, difG, difB));
}
}
pBoxMedian.Image = OutputPicture;
}
public void meanfilter(int p)
//KERNELSIZE=P
{
if (sliderKernel.Value % 2 == 0)
{
MessageBox.Show("Enter an odd number");
return;
}
Color ColorValue;
Bitmap InputPicture, OutputPicture;
InputPicture = new Bitmap(pBox_SOURCE.Image);
int PicWidth = InputPicture.Width;
int PicHeight= InputPicture.Height;
OutputPicture = new Bitmap(PicWidth, PicHeight);
OutputPicture = InputPicture;
int x, y, i, j, sumR, sumG, sumB, avgR, avgG, avgB;
for (x = (KernelSize - 1) / 2; x < PicWidth - (KernelSize - 1) / 2; x++)
{
for (y = (KernelSize - 1) / 2; y < PicHeight - (KernelSize- 1) / 2; y++)
{
toplamR = 0;
toplamG = 0;
toplamB = 0;
for (i = -((KernelSize - 1) / 2); i <= (KernelSize - 1) / 2; i++)
{
for (j = -((KernelSize - 1) / 2); j <= (KernelSize - 1) / 2; j++)
{
ColorValue= InputPicture.GetPixel(x + i, y + j);
sumR = sumR + ColorValue.R;
sumG = sumG + ColorValue.G;
sumB = sumB + ColorValue.B;
}
}
avgR = sumR / (KernelSize * KernelSize );
avgG = sumG / (KernelSize *KernelSize );
avgB = sumB / (KernelSize * KernelSize );
OutputPicture.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));
}
}
pBox_PROCESSED.Image = OutputPicture;
}
You compare y < PicWidth, whereas you probably want y < PicHeight. Is the image it worked on square, by chance?

c# change color for a picture, but only changed half.by gdi+

i have a picture is divided into four parts.i want change color by RGB.but only changed half.
1.change failed when width of the picture less than 256.
2.my small picture maximum not more than 256.
3.Every picture of 256*256 is normal.
4.problem value:r=0,g=0,b=52.
private void Adjust(Bitmap pBitmap, params int[] pValues)
{
BitmapData pBitmapData = pBitmap.LockBits(
new Rectangle(0, 0, pBitmap.Width, pBitmap.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
byte[] pData = new byte[pBitmapData.Stride * pBitmap.Height];
Marshal.Copy(pBitmapData.Scan0, pData, 0, pData.Length);
pBitmap.UnlockBits(pBitmapData);
int iOffset = pBitmapData.Stride - pBitmapData.Width * 3;
int iIndex = 0;
for (int i = 0; i < pBitmapData.Height; i++)
{
for (int j = 0; j < pBitmapData.Width; j++)
{
for (int k = iIndex; k < iIndex + 3; k++)
{
pData[k] = Adjust(pData[k], k);
}
iIndex += 3;
}
iIndex += iOffset;
}
Marshal.Copy(pData, 0, pBitmapData.Scan0, pData.Length);
//pBitmap.UnlockBits(pBitmapData);
}
protected byte Adjust(byte iValue, int iIndex)
{
int nColour = 0;
switch (iIndex % 3)
{
case 0:
nColour = (int)iValue + m_iBlue;
break;
case 1:
nColour = (int)iValue + m_iGreen;
break;
case 2:
nColour = (int)iValue + m_iRed;
break;
}
return nColour;
}
The original picture without using the problem parameters:
The original image using the problem parameters changed color:
Your problem, as I mentioned in my comment, is that the stride has the potential to mess up the %3 part, so you should pass which colour component you're reading as separate number, and not the full offset. The function doesn't need or use that offset anyway.
On a related note, the simplest way to iterate over an image by byte in my opinion is just by looping over X and Y, and just calculating the true offset inside the loop.
for (Int32 y = 0; y < pBitmapData.Height; y++)
{
for (Int32 x = 0; x < pBitmapData.Width; x++)
{
Int32 offset = y * pBitmapData.Stride + x * 3
for (int k = 0; k < 3; k++)
{
Int32 colOffset = offset + k;
pData[colOffset] = Adjust(pData[colOffset], k);
}
}
}
This way, you can just do a case switch on the actual k value as 0/1/2, and not the (offset+k)%3, which has no guarantees to be correct after the first line, since the stride usually rounds lines to a multiple of four bytes.
And finally... do boundary checking. You are editing bytes and those only go from 0 to 255.
protected Byte Adjust(Byte value, Int32 colourComponent)
{
Int32 newColour = 0;
switch (colourComponent)
{
case 0:
newColour = value + m_iBlue;
break;
case 1:
newColour = value + m_iGreen;
break;
case 2:
newColour = value + m_iRed;
break;
}
return (Byte)Math.Max(Math.Min(nColour, 255), 0);
}

the size of the image increases after applying steganography technique

i have problem with my image steganography code as it increases the size of the image after embeding the text.....for example if i load 10 kb pic as a input and embed 1 kb of text file then the resultent file size will be 150 kb aprox....thanks in advance
what is wrong with this piece of code???
public static Bitmap embedText(string text, Bitmap bmp)
{
State s = State.hiding;
int charIndex = 0;
int charValue = 0;
long colorUnitIndex = 0;
int zeros = 0;
int R = 0, G = 0, B = 0;
for (int i = 0; i < bmp.Height; i++)
{
for (int j = 0; j < bmp.Width; j++)
{
Color pixel = bmp.GetPixel(j, i);
pixel = Color.FromArgb(pixel.R - pixel.R % 2,
pixel.G - pixel.G % 2, pixel.B - pixel.B % 2);
R = pixel.R; G = pixel.G; B = pixel.B;
for (int n = 0; n < 3; n++)
{
if (colorUnitIndex % 8 == 0)
{
if (zeros == 8)
{
if ((colorUnitIndex - 1) % 3 < 2)
{
bmp.SetPixel(j, i, Color.FromArgb(R, G, B));
}
return bmp;
}
if (charIndex >= text.Length)
{
s = State.filling_with_zeros;
}
else
{
charValue = text[charIndex++];
}
}
switch (colorUnitIndex % 3)
{
case 0:
{
if (s == State.hiding)
{
R += charValue % 2;
charValue /= 2;
}
} break;
case 1:
{
if (s == State.hiding)
{
G += charValue % 2;
charValue /= 2;
}
} break;
case 2:
{
if (s == State.hiding)
{
B += charValue % 2;
charValue /= 2;
}
bmp.SetPixel(j, i, Color.FromArgb(R, G, B));
} break;
}
colorUnitIndex++;
if (s == State.filling_with_zeros)
{
zeros++;
}
}
}
}
return bmp;
}
Regardless of the input type extension, the image is converted to RGB for pixel manipulation. Since you directly change the pixels, what you want is to save the image with a lossless type, such as bitmap or png. Bitmap is uncompressed, so it takes up more space.

C# Array isn't holding data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to create a dungeon generator for a project I've been working on based off of this algorithm. I've gotten everything down, but my array (Fig. 1) doesn't seem to be holding giving the map data for some reason. I'm using three types of data to determine if a cell in the map is either empty (0), a space a character can be on (1), a hallway (2), or a wall (3).
I've gotten a bit stuck on this portion so any help is appreciated!
EDIT: The problem is the map object isn't storing the data in the loop shown in Fig. 1. Sorry for being so vague.
(Fig. 1)
for (int i = 0; i < roomList.Count; i++)
{
for (int x = roomList[i].X; x < (roomList[i].X + roomList[i].W); x++)
{
for (int y = roomList[i].Y; y < (roomList[i].Y + roomList[i].H); y++)
{
map[x, y] = 1;
}
}
}
(All of my relevant code)
namespace Project
{
}
public class Room
{
int xValue, yValue, widthValue, heightValue;
public int X
{
get { return xValue; }
set { xValue = value; }
}
public int Y
{
get { return yValue; }
set { yValue = value; }
}
public int W
{
get { return widthValue; }
set { widthValue = value; }
}
public int H
{
get { return heightValue; }
set { heightValue = value; }
}
}
public class DungeonGenerate
{
public int baseWidth = 513;
public int baseHeight = 513;
public int width = 64;
public int height = 64;
Color[,] arrayColor;
Random rand = new Random();
Room room = new Room();
Rectangle[,] rectMap;
public void Generate()
{
rectMap = new Rectangle[baseWidth, baseHeight];
//Creates a 2-D Array/Grid for the Dungeon
int[,] map = new int[baseWidth, baseHeight];
//Determines all the cells to be empty until otherwise stated
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
map[x, y] = 0;
}
}
//Determines the amount of rooms in the dungeon
int minRooms = (width * height) / 300;
int maxRooms = (width * height) / 150;
int amountOfRooms = rand.Next(minRooms, maxRooms);
//Room dimensions
int widthRoot = Convert.ToInt32(Math.Round(Math.Sqrt(width * 2)));
int heightRoot = Convert.ToInt32(Math.Round(Math.Sqrt(height * 2)));
int minWidth = Convert.ToInt32(Math.Round((width * .5) / widthRoot));
int maxWidth = Convert.ToInt32((width * 2) / widthRoot);
int minHeight = Convert.ToInt32(Math.Round(height * .5) / heightRoot);
int maxHeight = Convert.ToInt32((height * 2) / heightRoot);
//Creates the rooms
List<Room> roomList = new List<Room>(amountOfRooms);
for (int i = 0; i < amountOfRooms; i++)
{
bool ok = false;
do
{
room.X = rand.Next(width);
room.Y = rand.Next(height);
room.W = (rand.Next(maxWidth)) + minWidth;
room.H = (rand.Next(maxHeight)) + minHeight;
if (room.X + room.W >= width && room.Y + room.H >= height)
{
continue;
}
for (int q = 0; q < roomList.Count; q++)
{
if (room.X > roomList[q].X && room.X < roomList[q].X + room.W && room.Y > roomList[q].Y && room.Y < roomList[q].Y + room.H)
{
ok = false;
break;
}
}
ok = true;
roomList.Add(room);
} while (!ok);
}
//This will create hallways that lead to and from the rooms
int connectionCount = roomList.Count;
List<Point> connectedCells = new List<Point>((width * height));
for (int i = 0; i < connectionCount; i++)
{
Room roomA = roomList[i];
int roomNum = i;
while (roomNum == i)
{
roomNum = rand.Next(roomList.Count);
}
Room roomB = roomList[roomNum];
//Increasing this will make the hallway more straight, decreasing it will make the hallway more skewed
int sidestepChance = 10;
Point pointA = new Point(x: (rand.Next(roomA.W)) + roomA.X, y: (rand.Next(roomA.H)) + roomA.Y);
Point pointB = new Point(x: (rand.Next(roomB.W)) + roomB.X, y: (rand.Next(roomB.H)) + roomB.Y);
while (pointA != pointB)
{
int num = rand.Next() * 100;
if (num < sidestepChance)
{
if (pointB.X != pointA.X)
{
if (pointB.X > pointA.X)
{
pointB.X--;
}
else
{
pointB.X++;
}
}
}
else if(pointB.Y != pointA.Y)
{
if (pointB.Y > pointA.Y)
{
pointB.Y--;
}
else
{
pointB.Y++;
}
}
}
if (pointB.X < width && pointB.Y < height)
{
connectedCells.Add(pointB);
}
}
//Fills the room with data
for (int i = 0; i < roomList.Count; i++)
{
for (int x = roomList[i].X; x < (roomList[i].X + roomList[i].W); x++)
{
for (int y = roomList[i].Y; y < (roomList[i].Y + roomList[i].H); y++)
{
map[x, y] = 1;
}
}
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (map[x, y] == 0)
{
bool wall = false;
for (int yy = y - 2; yy < y + 2; yy++)
{
for (int xx = x - 2; xx < x + 2; xx++)
{
if (xx > 0 && yy > 0 && xx < width && yy < height)
{
if (map[xx, yy] == 1 || map[xx, yy] == 2)
{
map[x, y] = 3;
wall = true;
}
}
}
if (wall)
{
break;
}
}
}
}
}
//Rendering the Map and giving it some Color (Sort of)!
int scaler = baseWidth / width;
for (int x = 0; x < baseWidth; x++)
{
for (int y = 0; y < baseHeight; y++)
{
rectMap[x, y] = new Rectangle(x, y, 1, 1);
arrayColor = new Color[baseWidth, baseHeight];
switch (map[x, y])
{
case 0:
arrayColor[x, y] = new Color(0,0,0);
break;
case 1:
arrayColor[x, y] = new Color(0,0,0);
break;
case 2:
arrayColor[x, y] = new Color(0,0,0);
break;
case 3:
arrayColor[x, y] = new Color (0,0,0);
break;
}
}
}
}
public Rectangle[,] GetMap()
{
return rectMap;
}
public Color[,] GetColors()
{
return arrayColor;
}
}
In the for-loop where you're populating roomList, you're not instantiating a new Room each time. You're simply manipulating the same Room object and re-adding it to the list, so roomList will just contain many references to the same Room object. Try removing the room field from your DungeonGenerate class and use a local variable instead:
for (int i = 0; i < amountOfRooms; i++)
{
bool ok = false;
do
{
var room = new Room();
...
roomList.Add(room);
} while (!ok);
}

How to remove empty spaces from PNG images and re-save them

Alright i don't know this is possible programmatically or not with any software but lets learn it.
Now let me demonstrate it with example
i am using paint.net magic wand tolerance 0%
empty space
and here empty space removed version
is this possible to do via any software such as c# or photoshop etc
i need to do batch processing
ty
I do not really know whether this answers your question or not but I hope it does
Here's a code to remove surrounding white space from an image from Darren
public static Bitmap Crop(Bitmap bmp)
{
int w = bmp.Width;
int h = bmp.Height;
Func<int, bool> allWhiteRow = row =>
{
for (int i = 0; i < w; ++i)
if (bmp.GetPixel(i, row).R != 255)
return false;
return true;
};
Func<int, bool> allWhiteColumn = col =>
{
for (int i = 0; i < h; ++i)
if (bmp.GetPixel(col, i).R != 255)
return false;
return true;
};
int topmost = 0;
for (int row = 0; row < h; ++row)
{
if (allWhiteRow(row))
topmost = row;
else break;
}
int bottommost = 0;
for (int row = h - 1; row >= 0; --row)
{
if (allWhiteRow(row))
bottommost = row;
else break;
}
int leftmost = 0, rightmost = 0;
for (int col = 0; col < w; ++col)
{
if (allWhiteColumn(col))
leftmost = col;
else
break;
}
for (int col = w - 1; col >= 0; --col)
{
if (allWhiteColumn(col))
rightmost = col;
else
break;
}
if (rightmost == 0) rightmost = w; // As reached left
if (bottommost == 0) bottommost = h; // As reached top.
int croppedWidth = rightmost - leftmost;
int croppedHeight = bottommost - topmost;
if (croppedWidth == 0) // No border on left or right
{
leftmost = 0;
croppedWidth = w;
}
if (croppedHeight == 0) // No border on top or bottom
{
topmost = 0;
croppedHeight = h;
}
try
{
var target = new Bitmap(croppedWidth, croppedHeight);
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(bmp,
new RectangleF(0, 0, croppedWidth, croppedHeight),
new RectangleF(leftmost, topmost, croppedWidth, croppedHeight),
GraphicsUnit.Pixel);
}
return target;
}
catch (Exception ex)
{
throw new Exception(
string.Format("Values are topmost={0} btm={1} left={2} right={3} croppedWidth={4} croppedHeight={5}", topmost, bottommost, leftmost, rightmost, croppedWidth, croppedHeight),
ex);
}
}
Thanks,
I hope this helps :)

Categories

Resources