Is it possible to write to OpenGL texture 4 different planes - c

I'm trying to write data to different planes to opengl texture, is it possible?
I tried following code
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RED, GL_UNSIGNED_BYTE, (void*)&pixel);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_GREEN, GL_UNSIGNED_BYTE, (void*)&pixel);
It doesn't work as I expected, triangle is green instead of yellow, pixel value is 255.

No it is not. The missing channels are "filled" with 0.0 for red, green and blue, respectively 1.0 for the alpha channel.
See OpenGL 4.6 API Core Profile Specification - 8.4.4.4 Final Expansion to RGBA:
[...] Each group is converted to a group of 4 elements as follows: if a group does not contain an A element, then A is added and set to one for integer components or 1.0 for floating-point components. If any of R, G, or B is missing from the group, each missing element is added and assigned a value of 0 for integer components or 0.0 for floating-point components.

Related

Most efficient way to forward fill a bit array

Imagine you have a bit array (any data type is okay. e.g. list, np.array, bitarray, bitmap, etc of booleans) that is randomly filled. What is the fastest way to “forward fill” (left to right, or 0th index to nth index) that array in Python such that n bits get set to 1 following each bit already set to 1?
For example, take the array below:
[01000100000]
Given n=2 the forward filled array would be:
[01110111000]
edit
Assume that the input is a bit array of 10,000 elements, of which a random 20% are true, and n=25. This can be represented as a python list with 10,000 boolean elements, of which 20% are True. This could also be represented as a set with 2,000 int elements between 0 and 10,000.
edit 2
To get things started, here are some examples using the parameters above:
new = set()
new.update(*[range(i, i+25) for i in existing])
# 2.34 ms ± 56.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
new = BitMap() # This is a pyroaring BitMap
for e in existing:
new.add_range(e, e+25)
# 461 µs ± 6.02 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
I have addressed several datatypes below. There are no timings given, you might want to time the statement setting ans or refactor-in functions to time at the granularity that makes sense to you.
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 19 09:08:56 2021
for: https://stackoverflow.com/questions/70397220/most-efficient-way-to-forward-fill-a-bit-array
#author: paddy
"""
from random import sample
n = 2 # bits to the right of set bits to also set
elements = 17
true_percent = 20.0
#%% Using arbitrary precision int
print("\nUsing arbitrary precision int.\n".upper())
from operator import or_
from functools import reduce
# Set some random bits True
bits = sum(1 << r
for r in sample(range(elements), int(true_percent/100 * elements)))
# Set n right-adjacent bits.
ans = reduce(or_, (bits >> x for x in range(n+1)), 0)
# Print
print(f"Random bits = {bits:0{elements}b}")
if 1:
print()
for x in range(n+1):
print(f" {bits >> x:0{elements}b}")
print()
print(f"Answer = {ans:0{elements}b}\n")
#%% Using list.
print("\nUsing list.\n".upper())
from operator import or_
from functools import reduce
bits = [0] * elements
# Set some random bits to 1
for r in sample(range(elements), int(true_percent/100 * elements)):
bits[r] = 1
# Set n right-adjacent bits.
# [0]*x is padding bits on the left.
# zip(*(list1, list2,..)) returns the n'th elements on list1, list2,...
# int(any(...)) or's them.
ans = [int(any(shifts))
for shifts in zip(*([0]*x + bits for x in range(n+1)))]
# Print
print(f"Random bits = {bits}")
if 1:
print()
for x in range(n+1):
print(f" {[0]*x + bits}")
print()
print(f"Answer = {ans}\n")
#%% Using numpy.
# Adapt the list solution to use numpy operators on numpy arrays
#%% Using other ordered collections such as str.
# Convert to and from int solution.
Sample Output:
USING ARBITRARY PRECISION INT.
Random bits = 01000000010000010
01000000010000010
00100000001000001
00010000000100000
Answer = 01110000011100011
USING LIST.
Random bits = [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0]
Answer = [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0]

Tensorflowjs count black pixels from image

I am reading RGB images in tensorflow.js and need to find out the number of black pixels [0,0,0] in that image? Is there any method to do this operation?
A tensor image is a 3d tensor without the transparency
Given t a tensor image, the following will return the number of black pixels
t = tf.tensor([0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 1, 0], [2, 2, 3])
t.sum(-1).equal(tf.zeros(t.shape.slice(0, -1))).sum().print() // 2
We sum each pixel value (sum over the axis -1) and create a 2d tensor with same width and height as initial tensor. Both tensors are compared and the last tf.sum will count where the first tf.sum tensor has 0 values.

Need to highlight a certain element in array plot in mathematica

The array plot is constructed using a list. I want to mark the max intensity element with a red pointer. Is it possible?
Is this what you are looking for
m = {{1, 0, 0, 0.3}, {2, 7, 0, 0.3}, {1, 0, 1, 0.7}};
ArrayPlot[m /. Last[Sort[Flatten[m]]] -> Red]

how to create a pandas DataFrame by combining a list of column_names and a numpy array, and then adding more column(s)?

I have a list of names and a numpy array as below, respectively. How could I combine these two to make a pandas DataFrame? (My actual problem is larger than this, as I have more than 700 column names and hundred thousand inputs in the array). Your help will be so invaluable to me. Thank you.
column_names = [u'Bars', u'Burgers', u'Dry Cleaning & Laundry', u'Eyewear & Opticians', u'Local Services', u'Restaurants', u'Shopping']
values = array([[1, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 1, 0]], dtype=int64)
UPDATE
Thank you very much for the quick inputs. I am sorry that I did not fully explain the final goal that I would like to achieve -- I would like to add another column score, which is a list [4, 4.5, 5, 5.5, 3], to the pandas data frame. Then I would like to extract all columns except of score as predictors to predict score in a linear regression model. I think the essential part here is how to add a new column in an efficient way? I know that I can do
data = pd.DataFrame({"Bars": Bars, "Burgers": Burgers, "Dry Clearning & Laundry": Dry Cleaning & Laundry, ..."score": score})
However, this seems very unlikely to do as I have way too many columns.
I also use dd = pd.DataFrame(values, columns=column_names), and ddd = pd.DataFrame(dd, scores).
This yields:
Out[185]:
Bars Burgers Dry Cleaning & Laundry Eyewear & Opticians Local Services \
3 0.0 0.0 0.0 0.0 0.0
5 NaN NaN NaN NaN NaN
5 NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN
Restaurants Shopping
3 1.0 0.0
5 NaN NaN
5 NaN NaN
4 NaN NaN`
Once again thank you very much!!
import pandas as pd
import numpy as np
column_names = [u'Bars', u'Burgers', u'Dry Cleaning & Laundry', u'Eyewear & Opticians', u'Local Services', u'Restaurants', u'Shopping']
values = array([[1, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 1, 0]], dtype=int64)
df = pd.DataFrame(data=values, columns=column_names)
df.loc[:,'Scores'] = pd.Series(score, index=df.index)
I think I figured out. I can make scores another data frame. Then concatenate the first data frame dd = pd.DataFrame(values, columns=column_names) with the second data frame scores.
pd.concat([dd, scores], axis=1)
This can generate a new data frame.

How do I rotate values in a cvMat?

I am making a steerable derivative line filter. I want to be able to rotate it by an arbitrary angle. The desired situation will be the following:
cvMat myMat;
contains:
0, 0, 0
0, 0, 1
0, 0, 0
float angle = radians(45);
UnknownRotateMethod(myMat, angle);
myMat desired result:
0, 0, 0
0, 0, 0
0, 0, 1
(or similar due to discretization and aliasing)
Note I am using the C OpenCV API.
Edit: even though my example shows it, I would like to spell out that I want the rotation to be done on an axis point different from the (0,0). In this example I want the pivot to be at the center point of the matrix.
Solved:
cv2DRotationMatrix(pivot,angleDegrees,scale,outRotationMat);
cvWarpAffine(myMat,myMat,outRotationMat);

Resources